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.
题意:给你n个数,然后对于每个数,告诉你这个数的所有子节点,然后给你t组数据,对于每组数据(x,y),求他们的最近公共祖先,然后输入完t组数据后,求从1~n中每个数作为最近公共祖先出现的次数
思路:用targan三大算法之LCA算法求解这道题,首先开两个结构体,一个用来存原图,一个用来存需要求的图,先找到根节点,然后从根节点开始往下递归,开个vis[]数组来标记是否访问过,开个pre[]来记录父节点,其它的话就是主要用到targan的思想了
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
const int maxn=1005;
int head1[maxn],head2[maxn],f[maxn],pre[maxn],mark[maxn],vis[maxn];
int n,ant1,ant2;
struct lll
{
int v,next;
}p[maxn*maxn];
struct zzz
{
int v,next;
}q[maxn*maxn];
void add1(int u,int v)
{
p[ant1].v=v;
p[ant1].next=head1[u];
head1[u]=ant1++;
}
void add2(int u,int v)
{
q[ant2].v=v;
q[ant2].next=head2[u];
head2[u]=ant2++;
}
void init()
{
memset(head1,-1,sizeof(head1));
memset(head2,-1,sizeof(head2));
memset(f,-1,sizeof(f));
memset(mark,0,sizeof(mark));
memset(vis,0,sizeof(vis));
ant1=0,ant2=0;
for(int a=0;a<=n;a++)
{
pre[a]=a;
}
}
int found(int x)
{
return x==pre[x]?x:pre[x]=found(pre[x]);
}
void lca(int u,int fa)
{
for(int i=head1[u];i!=-1;i=p[i].next)
{
int v=p[i].v;
if(v==fa)
{
continue;
}
lca(v,u);
pre[v]=u; //标记父节点
}
vis[u]=1;
for(int i=head2[u];i!=-1;i=q[i].next)
{
int v=q[i].v;
if(vis[v])
{
mark[found(v)]++;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int a=1;a<=n;a++)
{
int u,x,v;
scanf("%d:(%d)",&u,&x);
for(int b=1;b<=x;b++)
{
scanf("%d",&v);
add1(u,v);
add1(v,u);
f[v]=u;
}
}
int root=1;
while(f[root]!=-1) //找到根节点
{
root=f[root];
}
int t;
scanf("%d",&t);
for(int a=1;a<=t;a++)
{
int x,y;
scanf(" (%d %d)",&x,&y);
add2(x,y);
add2(y,x);
}
lca(root,root);
for(int a=1;a<=n;a++)
{
if(mark[a])
{
printf("%d:%d\n",a,mark[a]);
}
}
}
return 0;
}