Language:
Closest Common Ancestors
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 |
一颗有向树 Q个查询 输出查询结果中每个点出现次数
answer记录每组查询u,v的LCA结果
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
const int MAXQ=500010;//查询数的最大值
//并查集部分
int F[MAXN];//初始化为-1
int find(int x)
{
if(F[x]==-1) return x;
return F[x]=find(F[x]);
}
void bing(int u,int v)
{
int t1=find(u);
int t2=find(v);
if(t1!=t2)
F[t1]=t2;
}
bool vis[MAXN];//访问标记
int ancestor[MAXN];//祖先
struct Edge
{
int to,next;
}edge[2*MAXN];
int head[MAXN],tot;
void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
struct Query
{
int q,next;
int index;//查询编号
}query[MAXQ*2];
int answer[MAXQ];//存储最后的查询结果 下标 0 Q-1
int h[MAXQ];
int tt;
int Q;
void add_query(int u,int v,int index)
{
query[tt].q=v;
query[tt].next=h[u];
query[tt].index=index;
h[u]=tt++;
query[tt].q=u;
query[tt].next=h[v];
query[tt].index=index;
h[v]=tt++;
}
void init()
{
tot=0;
MEM(head,-1);
tt=0;
MEM(h,-1);
MEM(vis,0);
MEM(F,-1);
MEM(ancestor,0);
}
void LCA(int u)
{
ancestor[u]=u;
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(vis[v]) continue;
LCA(v);
bing(u,v);
ancestor[find(u)]=u;
}
for(int i=h[u];i!=-1;i=query[i].next)
{
int v=query[i].q;
if(vis[v])
{
answer[query[i].index]=ancestor[find(v)];
}
}
}
bool flag[MAXN];
int Count_num[MAXN];
int main()
{
// fread;
int n;
while(scanf("%d",&n)!=EOF)
{
init();
MEM(flag,0);
for(int i=1;i<=n;i++)
{
int u,v,k;
scanf("%d:(%d)",&u,&k);
while(k--)
{
scanf("%d",&v);
flag[v]=1;
addedge(u,v);
addedge(v,u);
}
}
int Q;
scanf("%d",&Q);
for(int i=0;i<Q;i++)
{
char ch;
int u,v;
cin>>ch;
// scanf("%c",&ch);
scanf("%d %d)",&u,&v);
add_query(u,v,i);
}
int root;
for(int i=1;i<=n;i++)
if(!flag[i])
{
root=i;
break;
}
LCA(root);
MEM(Count_num,0);
for(int i=0;i<Q;i++)
Count_num[answer[i]]++;
for(int i=1;i<=n;i++)
if(Count_num[i]>0)
printf("%d:%d\n",i,Count_num[i]);
}
return 0;
}