Closest Common Ancestors
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 15676 | Accepted: 5019 |
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
解决方案:求节点做最近公共祖先的次数,直接套用模板即可,关键是怎么解决那个恶心的输入。
解决恶心输入1
code1:解决恶心输入2#include <iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=250005; int ehead[maxn],qhead[maxn]; int cnt[maxn],fa[maxn]; int cc[maxn]; bool vis[maxn]; int k,n; struct edge { int to,next; } E[maxn*2]; struct query { int to,next; } Q[maxn*2]; void addedge(int from,int to) { E[k].to=to; E[k].next=ehead[from]; ehead[from]=k++; } void addquery(int from,int to) { Q[k].to=to; Q[k].next=qhead[from]; qhead[from]=k++; } int Root(int x) { return x!=fa[x]?fa[x]=Root(fa[x]):x; } void Union(int u,int v) { int us=Root(u); int vs=Root(v); if(us!=vs) { fa[vs]=us; } } void init() { memset(vis,false,sizeof(vis)); memset(ehead,-1,sizeof(ehead)); memset(qhead,-1,sizeof(qhead)); memset(cc,0,sizeof(cc)); memset(cnt,0,sizeof(cnt)); for(int i=1; i<=n; i++) { fa[i]=i; } k=0; } void tarjan(int u) { vis[u]=true; for(int i=qhead[u]; i!=-1; i=Q[i].next) { int v=Q[i].to; if(vis[v]) { cnt[Root(v)]++; } } for(int i=ehead[u]; i!=-1; i=E[i].next) { int v=E[i].to; if(vis[v]) { continue; } tarjan(v); Union(u,v); } } int main() { while(~scanf("%d",&n)) { init(); for(int i=1; i<=n; i++) { int p,c; scanf("%d:(%d)",&p,&c); for(int i=0; i<c; i++) { int to; scanf("%d",&to); cc[to]++; addedge(p,to); addedge(to,p); } } int q; scanf("%d",&q); k=0; char str1[2],str2[2]; while(q--) { int u,v; scanf("%1s%d%d%1s",str1,&u,&v,str2);///可以滤去前后两个字符 addquery(u,v); addquery(v,u); } for(int i=1; i<=n; i++) { if(!cc[i]) { tarjan(i); } } for(int i=1; i<=n; i++) { if(cnt[i]) { printf("%d:%d\n",i,cnt[i]); } } } return 0; }
code2:
#include <iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=251005; int ehead[maxn],qhead[maxn]; int cnt[maxn],fa[maxn]; int cc[maxn]; bool vis[maxn]; int k,n; struct edge { int to,next; } E[maxn*2]; struct query { int to,next; } Q[maxn*2]; void addedge(int from,int to) { E[k].to=to; E[k].next=ehead[from]; ehead[from]=k++; } void addquery(int from,int to) { Q[k].to=to; Q[k].next=qhead[from]; qhead[from]=k++; } int Root(int x) { return x!=fa[x]?fa[x]=Root(fa[x]):x; } void Union(int u,int v) { int us=Root(u); int vs=Root(v); if(us!=vs) { fa[vs]=us; } } void init() { memset(vis,false,sizeof(vis)); memset(ehead,-1,sizeof(ehead)); memset(qhead,-1,sizeof(qhead)); memset(cc,0,sizeof(cc)); memset(cnt,0,sizeof(cnt)); for(int i=1; i<=n; i++) { fa[i]=i; } k=0; } void tarjan(int u) { vis[u]=true; for(int i=qhead[u]; i!=-1; i=Q[i].next) { int v=Q[i].to; if(vis[v]) { cnt[Root(v)]++; } } for(int i=ehead[u]; i!=-1; i=E[i].next) { int v=E[i].to; if(vis[v]) { continue; } tarjan(v); Union(u,v); } } int main() { while(~scanf("%d",&n)) { init(); for(int i=1; i<=n; i++) { int p,c; scanf("%d:(%d)",&p,&c); for(int i=0; i<c; i++) { int to; scanf("%d",&to); cc[to]++; addedge(p,to); addedge(to,p); } } int q; scanf("%d",&q); k=0; char str[100000]; int f=0; getchar(); while(1) { if(f==q) break; int u,v; gets(str); int len=strlen(str); for(int i=0; i<len; i++) { if(str[i]=='(') { u=0; int s; for(s=i+1; str[s]!=' '; s++) { if(str[s]!=' ') {u*=10; u+=str[s]-'0';} } v=0; s++; for(s;str[s]!=')';s++){ if(str[s]!=' ') {v*=10; v+=str[s]-'0';} } i=s; addquery(u,v); addquery(v,u); f++; } } } for(int i=1; i<=n; i++) { if(!cc[i]) { tarjan(i); } } for(int i=1; i<=n; i++) { if(cnt[i]) { printf("%d:%d\n",i,cnt[i]); } } } return 0; }