【LCA倍增模板】【poj1330】最近公共祖先

来源:互联网 发布:新手手机编程软件 编辑:程序博客网 时间:2024/05/22 01:57

Nearest Common Ancestors

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 31027

Accepted: 15800

Description

A rooted tree is a well-known data structure in computerscience and engineering. An example is shown below: 

 
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8is the root of the tree. Node x is an ancestor of node y if node x is in thepath between the root and node y. For example, node 4 is an ancestor of node16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4,10, and 16 are the ancestors of node 16. Remember that a node is an ancestor ofitself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called acommon ancestor of two different nodes y and z if node x is an ancestor of nodey and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors ofnodes 16 and 7. A node x is called the nearest common ancestor of nodes y and zif x is a common ancestor of y and z and nearest to y and z among their commonancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node4 is nearer to nodes 16 and 7 than node 8 is. 

For other examples, the nearest common ancestor of nodes 2 and 3 is node 10,the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest commonancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestorof z, then the nearest common ancestor of y and z is y. 

Write a program that finds the nearest common ancestor of two distinct nodes ina tree. 

Input

The input consistsof T test cases. The number of test cases (T) is given in the first line of theinput file. Each test case starts with a line containing an integer N , thenumber of nodes in a tree, 2<=N<=10,000. The nodes are labeled withintegers 1, 2,..., N. Each of the next N -1 lines contains a pair of integersthat represent an edge --the first integer is the parent node of the secondinteger. Note that a tree with N nodes has exactly N - 1 edges. The last lineof each test case contains two distinct integers whose nearest common ancestoris to be computed.

Output

Print exactly oneline for each test case. The line should contain the integer that is thenearest common ancestor.

Sample Input

2

16

1 14

8 5

10 16

5 9

4 6

8 4

4 10

1 13

6 15

10 11

6 7

10 2

16 3

8 1

16 12

16 7

5

2 3

3 4

3 1

1 5

3 5

Sample Output

4

3

(不翻译了不翻译了就是多组输入输出数据,每次x,y表示x是y的爸爸,最后一行询问)

#include<iostream>#include<queue>#include<cstdio>#include<algorithm>#define N 10050using namespace std;int T,n,m,tot;int head[N],next[N],v[N],depth[N],fa[N][21];void add_edge(int x,int y){    v[++tot]=y;    next[tot]=head[x];    head[x]=tot;}queue<int> q;void bfs(int root){    q.push(root);depth[root]=1;fa[root][0]=root;    while(!q.empty())    {        int x=q.front();q.pop();        for (int i=head[x];i;i=next[i])        {            int y=v[i];q.push(y);            depth[y]=depth[x]+1;            for (int j=1;j<=20;j++) fa[y][j]=fa[fa[y][j-1]][j-1];        }    }}int  lca(int x,int y){    if (depth[x]<depth[y]) swap(x,y);    int d=depth[x]-depth[y];    for (int i=0;i<=20;i++)        if ((1<<i)&d) x=fa[x][i];    for (int i=20;i>=0;i--)        if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];    return x==y?x:fa[x][0];}int main(){    cin>>T;    while(T--)    {        cin>>n;tot=0;        for (int i=0;i<n+10;i++)        {            head[i]=0;next[i]=0;v[i]=0;depth[i]=0;            for (int j=0;j<=20;j++) fa[i][j]=0;        }        for (int i=0;i<n-1;i++)        {            int x,y;            cin>>x>>y;            fa[y][0]=x;            add_edge(x,y);        }        int root;        for (int i=1;i<=n;i++)            if (fa[i][0] == 0)                {root=i;break;}        bfs(root);        int a,b;        cin>>a>>b;        cout<<lca(a,b)<<endl;    }    return 0;}
1 bfs记得把更新的点放在队列里

2 lca是要特判y是x祖先的时候

3 无根图要找根,也就是没有爸爸的点



原创粉丝点击