POJ——T 1470 Closest Common Ancestors

http://poj.org/problem?id=1470

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 20830   Accepted: 6617

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

Hint

Huge input, scanf is recommended.

Source

 
LCA ,,(多组数据、)
  1 #include <algorithm>
  2 #include <cstring>
  3 #include <cstdio>
  4 
  5 using namespace std;
  6 
  7 const int N(2e5+5);
  8 int n,m,cnt;
  9 int ans[N];
 10 
 11 int head[N],sumedge;
 12 struct Edge
 13 {
 14     int v,next;
 15     Edge(int v=0,int next=0):
 16         v(v),next(next){}
 17 }edge[N<<1];
 18 inline void ins(int u,int v)
 19 {
 20     edge[++sumedge]=Edge(v,head[u]);
 21     head[u]=sumedge;
 22 }
 23 
 24 int son[N],size[N],deep[N],top[N],dad[N],fa[N];
 25 void DFS(int u,int fa,int deepth)
 26 {
 27     size[u]=1;
 28     dad[u]=fa;
 29     deep[u]=deepth;
 30     for(int v,i=head[u];i;i=edge[i].next)
 31     {
 32         v=edge[i].v;
 33         if(dad[u]==v) continue;
 34         DFS(v,u,deepth+1);
 35         size[u]+=size[v];
 36         if(size[son[u]]<size[v]) son[u]=v;
 37     }
 38 }
 39 void DFS_(int u,int Top)
 40 {
 41     top[u]=Top;
 42     if(son[u]) DFS_(son[u],Top);
 43     for(int v,i=head[u];i;i=edge[i].next)
 44     {
 45         v=edge[i].v;
 46         if(dad[u]!=v&&son[u]!=v) DFS_(v,v);
 47     } 
 48 }
 49 int LCA(int x,int y)
 50 {
 51     for(;top[x]!=top[y];x=dad[top[x]])
 52         if(deep[top[x]]<deep[top[y]]) swap(x,y);
 53     return deep[x]<deep[y]?x:y;
 54 } 
 55 
 56 inline void init()
 57 {
 58     sumedge=0;
 59     memset(fa,0,sizeof(fa));
 60     memset(dad,0,sizeof(dad));
 61     memset(top,0,sizeof(top));
 62     memset(son,0,sizeof(son));
 63     memset(ans,0,sizeof(ans));
 64     memset(size,0,sizeof(size));
 65     memset(head,0,sizeof(head));
 66     memset(edge,0,sizeof(edge));
 67     memset(deep,0,sizeof(deep));
 68 }
 69 
 70 inline void read(int &x)
 71 {
 72     x=0;register char ch=getchar();
 73     for(;ch<'0'||ch>'9';) ch=getchar();
 74     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
 75 }
 76 
 77 int main()
 78 {
 79     for(int t;~scanf("%d",&t);init())
 80     {
 81         n=t;
 82         for(int u,v,nn;t--;)
 83         {
 84             read(u);
 85             read(nn);
 86             for(int i=1;i<=nn;i++)
 87             {
 88                 read(v);
 89                 fa[v]=u;
 90                 ins(u,v);
 91                 ins(v,u);
 92             }
 93         }
 94         int root=1;
 95         for(;root<=n;root++)
 96             if(!fa[root]) break;
 97         DFS(root,0,1);
 98         DFS_(root,root);
 99         read(m);
100         for(int u,v;m--;)
101         {
102             read(u),read(v);
103             ans[LCA(u,v)]++;
104         }
105         for(int i=1;i<=n;i++)
106             if(ans[i]) printf("%d:%d
",i,ans[i]);
107     }
108     return 0;
109 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7408565.html