Closest Common Ancestors - POJ 1470 LCA

来源:互联网 发布:美容护肤基础知识知乎 编辑:程序博客网 时间:2024/05/20 10:11

Closest Common Ancestors
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 15403 Accepted: 4919

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

55:(3) 1 4 21:(0)4:(0)2:(1) 33:(0)6(1 5) (1 4) (4 2)      (2 3)(1 3) (4 3)

Sample Output

2:15:5

题意:求两个点的最近公共祖先。

思路:LCA模板题。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<iostream>using namespace std;vector <int> G[1010];int parent[30][1010],depth[1010],root,vis[1010],ans[1010];char c,s[110];void kong(){ while(true)  { c=cin.peek();    if(c==' ' || c=='\n')     cin.ignore();    else     break;  }}void dfs(int v,int p,int d){ parent[0][v]=p;  depth[v]=d;  int i,len=G[v].size();  for(i=0;i<len;i++)   if(G[v][i]!=p)    dfs(G[v][i],v,d+1);}void init(int V){ dfs(root,-1,0);  int k,v;  for(k=0;k+1<30;k++)   for(v=0;v<V;v++)    if(parent[k][v]<0)     parent[k+1][v]=-1;    else     parent[k+1][v]=parent[k][parent[k][v]];}int lca(int u,int v){ int i,k;  if(depth[u]>depth[v])   swap(u,v);  for(k=0;k<30;k++)   if((depth[v]-depth[u])>>k &1)    v=parent[k][v];  if(u==v)   return u;  for(k=29;k>=0;k--)   if(parent[k][u]!=parent[k][v])   { u=parent[k][u];     v=parent[k][v];   }  return parent[0][u];}int main(){ int t=0,n,m,i,j,k,u,v;  while(~scanf("%d",&n))  { t++;    for(i=0;i<=n;i++)     G[i].clear();    for(i=1;i<=n;i++)    { scanf("%d:(%d)",&u,&m);      while(m--)      { scanf("%d",&v);        G[u].push_back(v);        vis[v]=t;      }    }    for(i=1;i<=n;i++)     if(vis[i]!=t)     { root=i;       break;     }    init(n+1);    memset(ans,0,sizeof(ans));    scanf("%d",&m);    for(i=1;i<=m;i++)    { kong();      scanf("%c",&c);      scanf("%d",&u);      kong();      scanf("%dc",&v,&c);      if(c!=')')      { kong();        scanf("%c",&c);      }      ans[lca(u,v)]++;    }    for(i=1;i<=n;i++)     if(ans[i]>0)      printf("%d:%d\n",i,ans[i]);  }}



0 0
原创粉丝点击