zoukankan      html  css  js  c++  java
  • poj 1330 A-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
    
     1 /*
     2 给你一棵树,然后给你两个点,让你求它们的最近公共祖先
     3 lac入门题,将第一个节点遍历到树根,并且将路径上的点标记为0
     4 然后让第二个节点也开始往树根方向遍历,当找到的节点为0时
     5 即为它们的最近公共祖先。
     6 */
     7 
     8 #include <stdio.h>
     9 #include <iostream>
    10 #include <string.h>
    11 #define M 10007
    12 using namespace std;
    13 int pre[M];
    14 
    15 int lca(int u,int v){
    16     if(u==v)
    17         return u;
    18     int w;
    19     while(u){      //从u(第一个例子中的16)递归到root(8)且将经过的节点标记下来
    20         w=pre[u];
    21         pre[u]=0;
    22         u=w;
    23     }
    24     for(;v;v=pre[v])
    25         if(pre[v]==0)
    26             return v;     //v(本例中6)递归,一直遇到u递归时被标记的第一个点
    27     return 0;
    28 }
    29 
    30 int main(){
    31     int t;
    32     scanf("%d",&t);
    33     while(t--){
    34         int n;
    35         scanf("%d",&n);
    36         int a,b,u,v;
    37         memset(pre,0,sizeof(pre));
    38         for(int i=1;i<n;i++){   //建树
    39             scanf("%d%d",&a,&b);
    40             pre[b]=a;
    41         }
    42         scanf("%d%d",&u,&v);
    43         int near=lca(u,v);
    44         printf("%d
    ",near);
    45     }
    46     return 0;
    47 }
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 #include <vector>
     5 #define N 100005
     6 using namespace std;
     7 vector <int> vec[N] ;
     8 vector <pair<int,int> > query[N];
     9 struct node{
    10     int first , second;
    11 }e;
    12 
    13 vector <node> query[N];
    14 
    15 int vis[N] , pre[N] , ans[N];
    16 int Find(int x){
    17     if(pre[x] == x){
    18         return x;
    19     }else{
    20         pre[x] = Find(pre[x]);
    21         return pre[x];
    22     }
    23 }
    24 void dfs(int u , int fa){
    25 
    26     pre[u] = u;
    27     vis[u] = 1;
    28     for(int i = 0 ; i < vec[u].size() ; i ++){
    29         int v = vec[u][i];
    30         if(v == fa) continue;
    31         dfs(v , u);
    32     }
    33 
    34     for(int i = 0 ; i < query[u].size() ; i++){
    35         int v = query[u][i].first;
    36         int id = query[u][i].second;
    37         if(vis[v] == 1){
    38             ans[id] = Find(v);
    39         }
    40     }
    41 
    42     pre[u] = fa;
    43 }
    44 
    45 int main(){
    46     int T;
    47     scanf("%d" ,&T);
    48     while(T --){
    49         int n , x, y;
    50         scanf("%d" , &n);
    51         for(int i = 0 ; i < n - 1 ; i ++){
    52             scanf("%d%d" , &x , &y);
    53             vec[x].push_back(y);
    54             vec[y].push_back(x);
    55             vis[y] = 1;
    56         }
    57         for(int i = 1 ; i <= n ; i ++ ){
    58             cout << i << "==========" << endl;
    59             for(int k = 0 ; k  <  vec[i].size() ; k ++){
    60                 cout << vec[i][k] << " ";
    61             }
    62             cout << endl;
    63         }
    64         int q;
    65         scanf("%d",&q);
    66         for(int i = 0 ; i < q ; i ++){
    67             scanf("%d%d" , &x , &y);
    68             query[x].push_back({y , i});
    69             query[y].push_back({x , i});
    70         }
    71         for(int i = 1 ; i <= n ; i ++){
    72             if(vis[i] == 0){
    73                 memset(vis , 0 ,sizeof(vis));
    74                 dfs(i , -1);
    75                 break;
    76             }
    77         }
    78         for(int i = 0 ; i < q ; i ++){
    79             printf("%d
    " ,ans[i]);
    80         }
    81     }
    82 }
  • 相关阅读:
    电赛小结
    markdown小结
    一元运算符重载
    二维数组作为函数参数传递剖析(转载)
    C语言内存(转载)
    Effective C++ chapter1:Accustiming Yourself to C++
    C++ 模板
    const
    命令行参数
    AStar算法
  • 原文地址:https://www.cnblogs.com/z-712/p/7324091.html
Copyright © 2011-2022 走看看