zoukankan      html  css  js  c++  java
  • POJ

    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
    
      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<algorithm>
      6 #include<queue>
      7 #include<stack>
      8 #include<deque>
      9 #include<map>
     10 #include<iostream>
     11 using namespace std;
     12 typedef long long  LL;
     13 const double pi=acos(-1.0);
     14 const double e=exp(1);
     15 const int N = 10100;
     16 
     17 #define lson i << 1,l,m
     18 #define rson i << 1 | 1,m + 1,r
     19 int cnt,ans;
     20 int a,b,n;
     21 int root;
     22 int head[N];
     23 int is_root[N];
     24 int father[N];
     25 int vis[N];
     26 
     27 struct edge
     28 {
     29     int to;
     30     int next;
     31 } edge[N];
     32 
     33 int seek(int ss)
     34 {
     35     int mid;
     36     int head=ss;
     37     while(ss!=father[ss])
     38         ss=father[ss];
     39 
     40     while(head!=ss)
     41     {
     42         mid=father[head];
     43         father[head]=ss;
     44         head=mid;
     45     }
     46     return ss;
     47 }
     48 
     49 void join(int xx,int yy)
     50 {
     51     int one=seek(xx);
     52     int two=seek(yy);
     53     if(one!=two)
     54         father[two]=one;  //注意把谁变成谁的上级
     55 }
     56 
     57 void add(int x,int y)
     58 {
     59     edge[cnt].to=y;
     60     edge[cnt].next=head[x];
     61     head[x]=cnt++;
     62 }
     63 
     64 void init()
     65 {
     66     int i,p,j;
     67     int x,y;
     68     cnt=0;
     69     memset(head,-1,sizeof(head));
     70     memset(is_root,0,sizeof(is_root));
     71     memset(vis,0,sizeof(vis));
     72     scanf("%d",&n);
     73     for(i=0; i<=n; i++)
     74         father[i]=i;
     75     for(i=1; i<n; i++)
     76     {
     77         scanf("%d%d",&x,&y);
     78         add(x,y);
     79         is_root[y]=1;
     80     }
     81     for(i=1; i<=n; i++)
     82         if(is_root[i]==0)
     83             root=i;
     84 }
     85 
     86 void LCA(int u)
     87 {
     88     int i,p,j;
     89     for(i=head[u]; i!=-1; i=edge[i].next)
     90     {
     91         int v=edge[i].to;
     92         LCA(v);
     93         join(u,v);
     94         vis[v]=1;
     95     }
     96 
     97     if(u==a&&vis[b]==1)
     98         ans=seek(b);
     99     if(u==b&&vis[a]==1)
    100         ans=seek(a);
    101 
    102     return ;
    103 }
    104 
    105 void solve()
    106 {
    107     scanf("%d%d",&a,&b);
    108     LCA(root);
    109 }
    110 
    111 int main()
    112 {
    113     int t,m,i,p,j;
    114     scanf("%d",&t);
    115     for(i=1; i<=t; i++)
    116     {
    117         init();
    118         solve();
    119 
    120         printf("%d
    ",ans);
    121     }
    122     return 0;
    123 }
    View Code
  • 相关阅读:
    用批处理来启动/停止SQL SERVER 2005的服务 【转载】
    c#命名法 【转】
    oracle 隐式游标,显示游标,游标循环,动态SELECT语句和动态游标,异常处理,自定义异常【转载】
    fetch bulk collect into 批量效率的读取游标数据 【转载】
    Oracle 外连接和 (+)号的用法 【转载】
    如何在Oracle中复制表结构和表数据 【转载】
    Oracle 小知识点
    VSS 2005 配置(含录像) 【转载】
    json 详解 【转】
    .NET 2.0 使用最新版的JSON.net 进行反序列化 【转载】
  • 原文地址:https://www.cnblogs.com/daybreaking/p/9427266.html
Copyright © 2011-2022 走看看