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

15 篇文章 0 订阅
4 篇文章 0 订阅

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 10050
using 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 无根图要找根,也就是没有爸爸的点



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值