LCA and examples

Seeking doubling method LCA

(Konjac only by multiplication)
It simply is a pretreatment of each node i to the depth of Deep [i] by its dfs section \ (2 ^ j \) ancestor f [i] [j]. Key requirements f [i] [j] is that recursive f [i] [j] = f [f [i] [j-1]] [j-1]. I.e. the i \ (2 ^ j \) ancestor \ (2 ^ {j-1 } \) ancestor \ (2 ^ {j-1 } \) ancestors.
Then seek \ (the LCA \) : First, the height of the two nodes is raised to the same level, while increasing and then same two parent node until the node. Of course, in order to ensure efficiency, should be promoted twice by doubling method.
The final space complexity O (nlogn), pretreatment time O (nlogn), a single query time O (logn)

const int N;//最多有N个节点
int n,deep[N],f[N][log_N];
vector<int> G[N];//无根树保存在这里

void dfs(int u,int ufa){ //预处理,ufa是u的父节点
    deep[u]=deep[ufa]+1;
    f[u][0]=ufa;
    for(int i=1;(1<<i)<=deep[u];i++)
        f[u][i]=f[f[u][i-1]][i-1];//求u的2^i祖先
    for(unsigned i=0;i<G[u].size();i++)
        if(G[u][i]!=ufa)
            dfs(G[u][i],u);//遍历u的所有儿子进行递归
int LCA(int u,int v){
    if(deep[u]<deep[v]) swap(u,v);//保证deep[u]>=deep[v]
    for(int i=log_n;i>=0;i--)//将u提到和v同一深度
        if(deep[u]>=deep[v]) u=f[u][i];
    if(u==v) return u;
    for(int i=log_n;i>=0;i--)//将u和v同时提升至其父节点相同
        if(f[u][i]!=f[v][i]){
            u=f[u][i];
            v=f[v][i];
        }
    return f[u][0];//此时u,v的父节点就是u,v的LCA
}

LCA applications

P3379 LCA template
P3884 binary problem
that Italy : a binary tree, find the depth and width (multilayer most node of nodes), and points u, v from the path (defined as the length by LCA u to v plus 2 to LCA path length).

Click to view the solution to a problem
Analysis : using LCA byproduct deep [i], may be most easily determined depth and width. As for the path distance, u, v and LCA is a long depth difference, according to the meaning of problems calculated.

P4281 emergency collection
that Italy : a given tree and three points (a plurality of sets of data), find the distance of three points and the minimum point and the sum of the distances.
Click to view the solution to a problem
Analysis : The analysis shown in FIG point must be found that the deepest points between each LCA. As shown, assuming that u = LCA (a, b) is three deepest LCA, then v = LCA (a, c) = LCA (b, c) will not be better than between u (uv more of length). Of course, the other two can find different LCA (LCA since at least two among three equal, unless u = v, are not necessarily the same as the other two u)

As the distance can be derived dis = deep [ a] + deep [b] + deep [c] -deep [u] -2 * deep [v]. And a u = LCA (a, b) , v = LCA (a, c) = LCA (b, c), Release dis = deep [a] + deep [b] + deep [c] -deep [LCA (a , b)] - deep [LCA (b, c)] - deep [LCA (c, a)], which also eliminates the trouble of classification discussed.

P3398 sugar hamster find
that Italy : given tree, and four numbers a, b, c, d (multiple sets of data). Asks the shortest path between the shortest path between ab and cd intersect.
Click to view the solution to a problem
Analysis : The path is clear is that the shortest path between two nodes with LCA. Since each node of the tree is only one father, then the two paths intersect if and only if a path of LCA on another path (or on the intersection of two fathers on both paths). Note L = LCA (u, v) , then how to determine the point p on the path uLv it?
This requires two conditions: 1.deep [p]> = deep [L]; 2.LCA (p, u) = L or LCA (p, v) = L . Of course, do not judge twice, just to ensure the first condition 1 is fulfilled (otherwise exchange) can then determine the condition 2.

Guess you like

Origin www.cnblogs.com/yhyxy/p/11565258.html
lca