一.查询一组的LCA
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 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5 #define MAX 10010
6 struct node
7 {
8 int to;
9 int next;
10 } edge[MAX];
11 int head[MAX];
12 int f[MAX];
13 int vis[MAX];
14 int is_root[MAX];
15 int n;
16 int cnt;
17 int cx,cy;
18 int ans;
19 int Find(int x)
20 {
21 if(x!=f[x])
22 f[x]=Find(f[x]);
23 return f[x];
24 }
25
26 void Join(int x,int y)///合并集合
27 {
28 int fx=Find(x);
29 int fy=Find(y);
30 if(fx!=fy)
31 {
32 f[fy]=fx;
33 }
34 }
35
36 void add_edge(int x,int y)
37 {
38 edge[cnt].to=y;
39 edge[cnt].next=head[x];
40 head[x]=cnt++;
41 }
42
43 void LCA(int u)
44 {
45 int i,v;
46 for(i=head[u]; i!=-1; i=edge[i].next)
47 {
48 v=edge[i].to;
49 LCA(v);
50 Join(u,v);
51 vis[v]=1;
52 }
53 if(cx==u&&vis[cy]==1)
54 {
55 ans=Find(cy);
56 }
57 if(cy==u&&vis[cx]==1)
58 {
59 ans=Find(cx);
60 }
61 }
62 int main()
63 {
64 int T,i;
65 int root;
66 scanf("%d",&T);
67 while(T--)
68 {
69
70 memset(head,-1,sizeof(head));
71 memset(vis,0,sizeof(vis));
72 memset(is_root,0,sizeof(is_root));
73 scanf("%d",&n);
74 cnt=0;
75 for(i=0; i<=n; i++)
76 f[i]=i;
77 for(i=1; i<n; i++)
78 {
79 int x,y;
80 scanf("%d%d",&x,&y);
81 add_edge(x,y);
82 is_root[y]=1;
83 }
84 for(i=1; i<=n; i++)///找根节点
85 {
86 if(is_root[i]==0)///入度为0的则是根节点
87 {
88 root=i;
89 }
90 }
91 scanf("%d%d",&cx,&cy);///单组查询
92 LCA(root);
93 printf("%d\n",ans);
94 }
95 return 0;
96 }
二.查询多组的LCA
Closest Common Ancestors
Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)
Input
The data set, which is read from a the std input, starts with the tree description, in the form:
nr_of_vertices
vertex:(nr_of_successors) successor1 successor2 ... successorn
...
where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form:
nr_of_pairs
(u v) (x y) ...
The input file contents several data sets (at least one).
Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.
Output
For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times
For example, for the following tree:
Sample Input
5
5:(3) 1 4 2
1:(0)
4:(0)
2:(1) 3
3:(0)
6
(1 5) (1 4) (4 2)
(2 3)
(1 3) (4 3)
Sample Output
2:1
5:5
Hint
Huge input, scanf is recommended.
这里使用链式前向星存图,又开了一个que数组来记录要查询的两个点存在关系。
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5 #define MAX 1010
6 struct node
7 {
8 int to;
9 int next;
10 } edge[MAX];
11 int head[MAX];
12 int f[MAX];
13 int vis[MAX];
14 int is_root[MAX];
15 int que[MAX][MAX];///新开一个数组记录要查询的两个点
16 int ans[MAX];
17 int n;
18 int cnt;
19 int cx,cy;
20 int Find(int x)
21 {
22 if(x!=f[x])
23 f[x]=Find(f[x]);
24 return f[x];
25 }
26
27 void Join(int x,int y)
28 {
29 int fx=Find(x);
30 int fy=Find(y);
31 if(fx!=fy)
32 {
33 f[fy]=fx;
34 }
35 }
36
37 void add_edge(int x,int y)
38 {
39 edge[cnt].to=y;
40 edge[cnt].next=head[x];
41 head[x]=cnt++;
42 }
43
44 void LCA(int u)
45 {
46 int i,v;
47 for(i=head[u]; i!=-1; i=edge[i].next)
48 {
49 v=edge[i].to;
50 LCA(v);
51 Join(u,v);
52 vis[v]=1;
53 }
54 for(i=1; i<=n; i++)///访问所有与u有关系的点
55 {
56 if(vis[i]&&que[u][i])
57 {
58 ans[Find(i)]+=que[u][i];
59 }
60 }
61 }
62 int main()
63 {
64 int T,i,j,t;
65 int root;
66 int x,y;
67 int num;
68 while(scanf("%d",&n)!=EOF)
69 {
70 memset(head,-1,sizeof(head));
71 memset(vis,0,sizeof(vis));
72 memset(is_root,0,sizeof(is_root));
73 memset(que,0,sizeof(que));
74 memset(ans,0,sizeof(ans));
75 cnt=0;
76 for(i=0; i<=n; i++)
77 {
78 f[i]=i;
79 }
80 for(i=1; i<=n; i++)
81 {
82
83 scanf("%d:(%d)",&x,&num);
84 for(j=1; j<=num; j++)
85 {
86 scanf("%d",&y);
87 add_edge(x,y);
88 is_root[y]=1;
89 }
90 }
91 scanf("%d",&t);
92 for(i=1; i<=t; i++)
93 {
94 scanf(" (%d %d)",&x,&y);
95 que[x][y]++;
96 que[y][x]++;
97 }
98 for(i=1; i<=n; i++)
99 {
100 if(is_root[i]==0)
101 {
102 root=i;
103 }
104 }
105 LCA(root);
106 for(i=1; i<=n; i++)
107 {
108 if(ans[i])
109 {
110 printf("%d:%d\n",i,ans[i]);
111 }
112 }
113 }
114 return 0;
115 }
作者:王陸