zoukankan      html  css  js  c++  java
  • poj-1330 Nearest Common Ancestors

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

     
    In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. 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 of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 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 common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of 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 in a tree. 

    Input

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

    Output

    Print exactly one line for each test case. The line should contain the integer that is the nearest 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

     LCA模板题

    tarjan

    //#include <bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<queue>
    using namespace std;
    typedef long long ll;
    #define inf 2147483647
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    #define ri register int
    template <class T> inline T min(T a, T b, T c)
    {
        return min(min(a, b), c);
    }
    template <class T> inline T max(T a, T b, T c)
    {
        return max(max(a, b), c);
    }
    template <class T> inline T min(T a, T b, T c, T d)
    {
        return min(min(a, b), min(c, d));
    }
    template <class T> inline T max(T a, T b, T c, T d)
    {
        return max(max(a, b), max(c, d));
    }
    #define pi acos(-1)
    #define me(x, y) memset(x, y, sizeof(x));
    #define For(i, a, b) for (int i = a; i <= b; i++)
    #define FFor(i, a, b) for (int i = a; i >= b; i--)
    #define bug printf("***********
    ");
    #define mp make_pair
    #define pb push_back
    const int N = 1e5+5;
    const int M=200005;
    // name*******************************
    struct edge
    {
        int to,nxt,idx;
    } e[N<<1],e_[N];
    int tot=1;
    int tot_=0;
    int fa[N];
    int fst[N];
    int fst_[N];
    int rk[N];
    int n,m,T;
    int acr[N];
    int ins[N];
    bool flag[N];
    int ans[N];
    // function******************************
    void init()
    {
        tot=1;
        tot_=0;
        me(ins,0);
        For(i,1,n)fa[i]=i;
        me(rk,0);
        me(acr,0);
        me(fst,0);
        me(fst_,0);
        me(flag,0);
    }
    void add(int u,int v)
    {
        e[++tot].to=v;
        e[tot].nxt=fst[u];
        fst[u]=tot;
    }
    void add_(int u,int v,int t)
    {
        e_[++tot_].to=v;
        e_[tot_].nxt=fst_[u];
        e_[tot_].idx=t;
        fst_[u]=tot_;
    }
    int find(int x)
    {
        return x==fa[x]?x:fa[x]=find(fa[x]);
    }
    void uone(int a,int b)
    {
        int t1=find(a),t2=find(b);
        if(t1!=t2)
        {
            if(rk[t1]>rk[t2])fa[t2]=t1;
            else fa[t1]=t2;
            if(rk[t1]==rk[t2])rk[t2]++;
        }
    }
    void LCA(int u)
    {
        acr[u]=u;
        ins[u]=1;
        for(int p=fst[u]; p; p=e[p].nxt)
        {
            int v=e[p].to;
            if(ins[v])continue;
            LCA(v);
            uone(u,v);
            acr[find(u)]=u;
        }
        for(int p=fst_[u]; p; p=e_[p].nxt)
        {
            int v=e_[p].to;
            if(ins[v])ans[e_[p].idx]=acr[find(v)];
        }
    }
    
    //***************************************
    int main()
    {
    //    ios::sync_with_stdio(0);
    //    cin.tie(0);
        // freopen("test.txt", "r", stdin);
        //  freopen("outout.txt","w",stdout);
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            int u,v;
            init();
            For(i,1,n-1)
            {
                scanf("%d%d",&u,&v);
                add(u,v);
                add(v,u);
                flag[v]=1;
            }
            For(i,1,1){
            scanf("%d%d",&u,&v);
            add_(u,v,i);
            add_(v,u,i);
            }
            int pos;
            For(i,1,n)
            if(!flag[i])
            {
                pos=i;
                break;
            }
    
            LCA(pos);
            For(i,1,1)
            printf("%d
    ",ans[i]);
    
        }
        return 0;
    }

    倍增

    //#include <bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<queue>
    using namespace std;
    typedef long long ll;
    #define inf 2147483647
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    #define ri register int
    template <class T> inline T min(T a, T b, T c)
    {
        return min(min(a, b), c);
    }
    template <class T> inline T max(T a, T b, T c)
    {
        return max(max(a, b), c);
    }
    template <class T> inline T min(T a, T b, T c, T d)
    {
        return min(min(a, b), min(c, d));
    }
    template <class T> inline T max(T a, T b, T c, T d)
    {
        return max(max(a, b), max(c, d));
    }
    #define pi acos(-1)
    #define me(x, y) memset(x, y, sizeof(x));
    #define For(i, a, b) for (int i = a; i <= b; i++)
    #define FFor(i, a, b) for (int i = a; i >= b; i--)
    #define bug printf("***********
    ");
    #define mp make_pair
    #define pb push_back
    const int N = 1e5+5;
    const int M=200005;
    // name*******************************
    struct edge
    {
        int to,nxt;
    } e[N];
    int n;
    int tot=1;
    int fst[N];
    int dep[N];
    int in[N];
    int acr[N][31];
    int T;
    int flag[N];
    // function******************************
    void add(int u,int v)
    {
        e[++tot].to=v;
        e[tot].nxt=fst[u];
        fst[u]=tot;
    }
    void dfs(int u,int f,int deep)
    {
        dep[u]=deep;
        acr[u][0]=f;
        for(int p=fst[u]; p; p=e[p].nxt)
        {
            int v=e[p].to;
            if(!dep[v])
                dfs(v,u,deep+1);
        }
    }
    void init() //计算深度
    {
        for(int j=1; (1<<j)<=n; j++)
            for(int i=1; i<=n; i++)
                acr[i][j]=acr[acr[i][j-1]][j-1];
    }
    int LCA(int u,int v)
    {
        if(dep[u]<dep[v])//交换位置
            u=u^v,v=u^v,u=u^v;
        int d=dep[u]-dep[v];
        for(int i=0; (1<<i)<=d; i++)//计算好深度差就让u往上移到相等深度位置,这里恰好用到移位运算
            if((1<<i)&d)
                u=acr[u][i];
        if(u==v)return u;
        FFor(i,20,0)//不断往上更新顶
        if(acr[u][i]!=acr[v][i])//因为相等不代表最近公共父节点
            u=acr[u][i],v=acr[v][i];
        return acr[u][0];
    }
    void CLS()
    {
        me(fst,-1);
        me(dep,0);
        tot=0;
        me(flag,0);
    }
    //***************************************
    int main()
    {
    //    ios::sync_with_stdio(0);
    //    cin.tie(0);
        // freopen("test.txt", "r", stdin);
        //  freopen("outout.txt","w",stdout);
        scanf("%d",&T);
        while(T--)
        {
            CLS();
            scanf("%d",&n);
            int u,v;
            init();
            For(i,1,n-1)
            {
                scanf("%d%d",&u,&v);
                add(u,v);
                add(v,u);
                flag[v]=1;
            }
    
            int root;
            For(i,1,n)
            if(!flag[i])
            {
                root=i;
                break;
            }
            dfs(root,-1,1);
            init();
            scanf("%d%d",&u,&v);
            printf("%d
    ",LCA(u,v));
        }
        return 0;
    }
  • 相关阅读:
    Chrome Native Messaging 与本地程序之间的通信
    由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
    linux
    maven POM.xml 标签详解 转
    java-线程观察整个生命周期
    java-Web项目开发中Spring整合Junit单元测试
    java-Map集合的四种遍历方式
    Java-8内置的核心函数式接口接口
    正则表达式
    Java-Atomic系列12大类实例讲解和原理分解
  • 原文地址:https://www.cnblogs.com/planche/p/8904570.html
Copyright © 2011-2022 走看看