poj1330

大兔子大兔子 提交于 2019-12-02 09:31:09

 

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 }
离线版本

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!