圆方树

· · 算法·理论

场景:计算无向图上简单路径必经点和可经点信息或相关信息。

将每一个点双抽象成一个代表点(方点),然后与其内部的点两两相连。

圆方树特性:

路径可经点个数(包括必经点):方点度数 - 圆点 +2。

实现:

int low[N], dfn[N], tar, st[N], top, cnt;
vector<int> rs_mp[N], mp[N];
void rs_add(int x, int y) { rs_mp[x].push_back(y), rs_mp[y].push_back(x); }
void rs_tarjan(int x, int f)
{
    low[x] = dfn[x] = ++tar, st[++top] = x;
    for (auto i : mp[x])
    {
        if (i == f)
            continue;
        if (!dfn[i])
        {
            rs_tarjan(i, x);
            low[x] = min(low[x], low[i]);
            if (low[i] >= dfn[x])
            {
                rs_add(x, ++cnt);
                do
                {
                    rs_add(st[top], cnt);
                } while (st[top--] != i);
            }
        }
        else
            low[x] = min(low[x], dfn[i]);
    }
}
void init()
{
    cnt = n; // n 为原图点数
    for (int i = 1; i <= n; i++)
        if (!dfn[i]) // 图可能不连通,建立圆方森林
            rs_tarjan(i, 0);
}