zoukankan      html  css  js  c++  java
  • poj1330

     

    POJ1330 Nearest Common Ancestors

     

    Description

    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

     

     

    【题意】:T组数据求询问单次LCA裸题,练手用。

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <cmath> 
     4 #include <algorithm>
     5 #include <string>
     6 #include <string.h>
     7 using namespace std;
     8 int quex,quey;
     9 int du[100000+1]; 
    10 bool v[100000+1]={};
    11 int fa[100000+1];
    12 int ans;
    13 struct node
    14 {
    15     int next;int to;
    16 }e[100000+1];
    17 int cnt;
    18 int g[100000+1];
    19 int n;
    20 bool flag;
    21 inline void add(int x,int y)
    22 {
    23     e[++cnt].next=g[x];
    24     g[x]=cnt;
    25     e[cnt].to=y;
    26 }
    27 
    28 int getfa(int x)
    29 {
    30     return (fa[x]==x?x:getfa(fa[x]));
    31 }
    32 inline void merge(int x,int y)
    33 {    
    34     fa[getfa(x)]=fa[getfa(y)];
    35 }
    36 inline void tarjan(int now)
    37 {
    38     int i;
    39     v[now]=1;
    40     if (v[quex]  && !flag && now==quey)
    41       {ans=getfa(quex);flag=1;}
    42     if (v[quey]  && !flag && now==quex)
    43       {ans=getfa(quey);flag=1;}
    44     for (i=g[now];i;i=e[i].next)
    45       if (!v[e[i].to]) 
    46         {
    47         
    48           tarjan(e[i].to);
    49           fa[e[i].to]=now; 
    50        }
    51 }
    52 inline void  release()
    53 {
    54     for (register int i=1;i<=n;i++) fa[i]=i;
    55     cnt=0;
    56     flag=0;
    57     memset(g,0,sizeof (g));
    58     memset(du,0,sizeof(du));
    59     memset(v,0,sizeof(v));
    60 }
    61 int main()
    62 {
    63     int t;
    64     scanf("%d",&t);
    65     while(t--)
    66       {
    67           scanf("%d",&n);
    68           release();
    69           int root;
    70           for (register int i=1;i<=n-1;i++)
    71             {
    72                 int x ,y;
    73             scanf("%d%d",&x,&y);
    74             du[y]++;
    75               add(x,y);
    76             }
    77           for (register int i=1;i<=n;i++)
    78             if (!du[i]) {root=i;break;}
    79           scanf("%d%d",&quex,&quey);
    80           tarjan(root);
    81           cout<<ans<<endl;
    82       }
    83       return 0;
    84 }
    离线版本
     1 #include <iostream>
     2 #include <string>
     3 #include <string.h>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <stdio.h>
     7 using namespace std;
     8 int n;
     9 int du[100000+1]={};
    10 int deep[100000+1];
    11 int fa[100000+1][21];
    12 int g[100000+1];
    13 int cnt=0;
    14 int maxdeep;
    15 struct node
    16 {
    17     int next;int to;
    18 }e[100000+1];
    19 int read()
    20 {
    21     int num=0;int fh=1;char ch=getchar();
    22     while(ch<'0' || ch>'9'){if (ch=='-') fh=-1;ch=getchar();}
    23     while(ch<='9' && ch>='0'){num=(num<<1)+(num<<3)+ch-'0';ch=getchar();}
    24     return (num*fh);
    25 }
    26 inline void add(int x,int y)
    27 {
    28     e[++cnt].next=g[x];
    29     g[x]=cnt;
    30     e[cnt].to=y;
    31 }
    32 void dfs(int u)
    33 {
    34     for (register int i=g[u];i;i=e[i].next)
    35       deep[e[i].to]=deep[u]+1,fa[e[i].to][0]=u,maxdeep=max(maxdeep,deep[e[i].to]),dfs(e[i].to);
    36 }
    37 void bz()
    38 {
    39     int i,j;
    40     for (j=1;(1<<j)<=maxdeep;j++)
    41       for (i=1;i<=n;i++)
    42        fa[i][j]=fa[fa[i][j-1]][j-1];
    43 }
    44 int query(int u,int v)
    45 {
    46     int j;
    47     if (deep[v]>deep[u]) swap(u,v);
    48     for (j=20;j>=0;j--)
    49       if (deep[u]-deep[v]>=(1<<j)) u=fa[u][j]; 
    50     if (u==v) return u;
    51     for (j=20;j>=0;j--)
    52       if (fa[u][j]!=fa[v][j])
    53         u=fa[u][j],v=fa[v][j];
    54     return fa[u][0];
    55 }
    56 inline void release()
    57 {
    58     memset(g,0,sizeof (g));
    59     cnt=0;
    60     memset(du,0,sizeof (du));
    61     memset(deep,0,sizeof (deep));
    62     memset(fa,0,sizeof(fa));
    63 }
    64 int main()
    65 {
    66     int t,i;
    67     
    68     t=read();
    69     while(t--)
    70     {
    71         n=read();
    72         release();
    73         for (i=1;i<=n-1;i++)
    74           {
    75               int x=read(),y=read();
    76               add(x,y);
    77               du[y]++;
    78           }
    79         for (i=1;i<=n;i++)
    80           if (!du[i]) break;
    81         deep[i]=1;
    82         dfs(i);
    83         bz();
    84         int quex=read(),quey=read();
    85         cout<<query(quex,quey)<<endl;
    86     }
    87     return 0;
    88 }
    在线版本
  • 相关阅读:
    疫情控制
    数据结构1
    NOIP 模拟 921
    ml-agents项目实践(一)
    Appium的安装及简单的使用介绍
    移动设备管理平台的搭建(基于STF/ATXServer2)
    ClickHouse利器—如何提高留存计算速度
    Linux基本操作命令
    深度学习与强化学习的两大联姻:DQN与DDPG的对比分析
    漏洞扫描软件AWVS的介绍和使用
  • 原文地址:https://www.cnblogs.com/yz12138/p/11741214.html
Copyright © 2011-2022 走看看