dfs序

dfs序表示每个节点在dfs时进出栈的时间序列。

dfs序可以把一棵树区间化,可求出每个节点管辖的区间。

同一颗子树所对应的一定是dfs序中连续的一段。

int L[maxn], R[maxn], dep[maxn], cur;
vector<int> G[maxn];

void dfs(int x, int fa, int d)
{
    L[x] = ++cur;
    dep[x] = d;
    for(auto y : G[x])
    {
        if(y == fa) continue;
        dfs(y, x, d + 1);
    }
    R[x] = cur;
}

dep[x]为x的深度,L[x]为dfs序中x子树开始的位置,R[x]为dfs序中x子树的结束位置

 

posted @ 2020-10-18 10:42  .Ivorelectra  阅读(108)  评论(0编辑  收藏  举报