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.
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
题意:首先给了n个点,然后接下来n行描述了每一个点的儿子信息,接下来有q组查询,最后让你统计一下每个顶点作为最近公共祖先的次数
思路:Tarjan离线 记录每个节点最为最近公共祖先的次数
/*
*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <map>
#include <vector>
#include <set>
#include <bitset>
#include <stack>
#define ull unsigned long long
#define mems(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const ll mod=1000000007;
const double pi=acos(-1);
const int N=1004;
struct node//建树连边
{
int u,v,next;
} g[N*N];
struct nod
{
int u,v,next;
} G[N*N];
int n,m,ans;
int head[N],hd[N];
int tot;
int res[N];//记录次数
int vis[N],pre[N],fa[N];
void init(int n)
{
tot=0;
memset(vis,0,sizeof(vis));
mems(fa,-1);
mems(res,0);
memset(head,-1,sizeof(head));
memset(hd,-1,sizeof(hd));
// memset(g,0,sizeof(g));
// memset(G,0,sizeof(G));//会内存超限?
for(int i=1;i<=n;i++)
pre[i]=i;
}
void addg(int u,int v)
{
g[tot].u=u;
g[tot].v=v;
g[tot].next=head[u];
head[u]=tot++;
}
void addG(int u,int v)
{
G[tot].u=u;
G[tot].v=v;
G[tot].next=hd[u];
hd[u]=tot++;
}
int Find(int x)
{
if(x==pre[x])
return x;
else
return pre[x]=Find(pre[x]);
}
void Lca(int u,int fa)
{
for(int i=head[u]; ~i; i=g[i].next)
{
int v=g[i].v;
if(v==fa)
continue;
if(!vis[v])
{
Lca(v,u);
pre[v]=u;
}
}vis[u]=1;
for(int i=hd[u]; ~i; i=G[i].next)
{
int v=G[i].v;
if(vis[v])
{
res[Find(v)]++;//最近的公共祖先Find(v)
}
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init(n);
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);
addg(a,c);
addg(c,a);
fa[c]=a;
}
}
int root=1;//寻找根节点
while(fa[root]!=-1)
root=fa[root];
int m;
scanf("%d",&m);
tot=0;
for(int i=0; i<m; i++)
{
scanf(" (%d %d)",&a,&b);
addG(a,b);
addG(b,a);
}
Lca(root,root);
for(int i=1; i<=n; i++)
if(res[i])
printf("%d:%d\n",i,res[i]);
}
return 0;
}