• POJ Nearest Common Ancestors (RMQ+树上dfs序求LCA)


    题面

    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

    思路

    就是普通的RMQ求lca板子。不过记得deep数组和ver数组要开的大一点,因为dfs序列应该蛮大的。

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<bitset>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=start;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    const int maxn=1e4+10;
    int n,m,t;
    int cnt=0,tot=0;
    int dp[maxn*2][25],ver[maxn*2],vis[maxn*2];
    int first[maxn],deep[maxn*2],head[maxn];
    int is_root[maxn];
    
    struct node {
        int v,next;
    }e[maxn<<1];
    
    inline void add_edge (int u,int v) {
        e[++cnt].v=v;
        e[cnt].next=head[u];
        head[u]=cnt;
    }
    
    void dfs (int x,int dep) {
        vis[x]=true;
        ver[++tot]=x;
        first[x]=tot;
        deep[tot]=dep;
        for (int i=head[x];i;i=e[i].next) {
            int v=e[i].v;
            if (!vis[v]) {
                dfs (v,dep+1);
                ver[++tot]=x;
                deep[tot]=dep;
            }
        }
    }
    
    void ST (int n) {
        rep (i,1,n) dp[i][0]=i;
        for (int j=1;(1<<j)<=n;j++) 
         for (int i=1;i<=n-(1<<j)+1;i++) {
            int a=dp[i][j-1],b=dp[i+(1<<(j-1))][j-1];
            dp[i][j]=deep[a]<deep[b]?a:b;
        }
    }
    
    int RMQ (int l,int r) {
        int k=__lg (r-l+1);
        int a=dp[l][k],b=dp[r-(1<<k)+1][k];
        return deep[a]<deep[b]?a:b;
    }
    
    int LCA (int x,int y) {
        int a=first[x],b=first[y];
        if (a>b) swap (a,b);
        int ans=RMQ (a,b);
        return ver[ans];
    }
    
    inline void init () {
        MT (vis,0); cnt=0,tot=0;
        MT (is_root,true); MT (deep,0);
        MT (ver,0); MT (head,0);
        MT (dp,0); MT (first,0);
    }
    
    int main () {
        scanf ("%d",&t);
        while (t--) {
            init ();
            scanf ("%d",&n);
            rep (i,1,n-1) {
                int x,y;
                scanf ("%d%d",&x,&y);
                is_root[y]=false;
                add_edge (x,y);
                add_edge (y,x);
            }
            int root;
            rep (i,1,n) if (is_root[i]) {
                root=i;
                break;
            }
            dfs (root,1);
            ST (2*n-1);
            int x,y;
            scanf ("%d%d",&x,&y);
            printf ("%d
    ",LCA (x,y));
        }
    }
    
    
  • 相关阅读:
    松软科技web课堂:SQLServer之ROUND() 函数
    松软科技web课堂:SQLServer之LEN() 函数
    接口工具比较
    记录EXCEL格式和TXT文本格式之间的互转
    Fiddler安装证书
    Nginx概述及安装配置
    YSLOW(一款实用的网站性能检测工具)
    通用测试用例大全
    Macaca环境配置及样例执行
    ADB命令
  • 原文地址:https://www.cnblogs.com/hhlya/p/13752271.html
Copyright © 2020-2023  润新知