Description
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.
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:
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.
这道题感觉比A题要简单些,题意就是统计哪些点当了多少次祖先,然后依次输出,因为他的输入是按照某点及儿子一起输入的,所以可以用动态数组vector来存每个点的儿子,因为是统计,不需要一对一的输出,所以可以将需要查询的问题存在一个二维数组里,一次dfs后就可以完成所有的查询,还有一个很重要的就是,一定要用scanf,即使加了/*ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/这个cin也不能过。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define maxn 1000
using namespace std;
int n,num,cnt;
vector <int> anc[maxn];
bool isroot[maxn],vis[maxn];
int all[maxn],fa[maxn],que[1000][1000];
int Find(int a)
{
if(a!=fa[a])
a=Find(fa[a]);
return a;
}
void dfs(int root)
{
vis[root]=true;
fa[root]=root;
int len=anc[root].size();
for(int i=0;i<len;i++)
{
int t=anc[root][i];
dfs(t);
fa[t]=root;
}
for(int i=1;i<=n;i++)
{
if(vis[i]&&que[root][i])
{
all[Find(i)]+=que[root][i];
que[root][i]=0; //查询后要把两个都清零,避免重复查询
que[i][root]=0;
}
}
}
int main()
{
/*ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
anc[i].clear();
}
memset(isroot,true,sizeof(isroot));
memset(all,0,sizeof(all));
memset(vis,false,sizeof(vis));
memset(que,0,sizeof(que));
int a,b,c;
for(int i=0;i<n;i++)
{
scanf("%d:(%d)",&a,&b);
for(int j=0;j<b;j++)
{
scanf(" %d",&c);
anc[a].push_back(c);
isroot[c]=false;
}
}
int q,u,v,root;
scanf("%d",&q);
for(int j=1;j<=n;j++)
{
if(isroot[j])
{
root=j;
break;
}
}
for(int i=0;i<q;i++)
{
scanf(" (%d %d)",&u,&v);
que[u][v]++; //为了简单一点,可以不去查询哪个点所在的深度更深,可以正反都存起来
que[v][u]++;
}
dfs(root); //dfs一定是从根节点开始查,所以要先找出根节点
for(int i=1;i<=n;i++)
{
if(all[i])
printf("%d:%d\n",i,all[i]);
}
}
return 0;
}