POJ---1470- Closest Common Ancestors

来源:互联网 发布:淘宝免单群教程 编辑:程序博客网 时间:2024/06/05 11:06

Language:
Closest Common Ancestors
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 17367 Accepted: 5573

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

Hint

Huge input, scanf is recommended.

Source

Southeastern Europe 2000



#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <list>#include <map>using namespace std;const int SIZE=1100;int set[SIZE];vector<int> tree[SIZE];int poi[SIZE][SIZE],ans[SIZE];int judge[SIZE],n;int set_find(int x){    if(set[x]<0)return x;    return set[x]=set_find(set[x]);}void Tarjan(int u){    int i;    for(i=0; i<tree[u].size(); i++)    {        Tarjan(tree[u][i]);        set[tree[u][i]]=u;    }    judge[u]=1;    for(i=1; i<=n; i++)        if(judge[i] && poi[u][i])            ans[set_find(i)]+=poi[u][i];}int main(){    int m,i;    int a,b;    while(~scanf("%d",&n))    {        for(i=1; i<=n; i++)            tree[i].clear();        memset(poi,0,sizeof(poi));        memset(judge,0,sizeof(judge));        memset(ans,0,sizeof(ans));        memset(set,0,sizeof(set));        for(i=0; i<n; i++)        {            scanf("%d:(%d)",&a,&m);            while(m--)            {                scanf("%d",&b);                tree[a].push_back(b);                set[b]=-1;            }        }        scanf("%d",&m);        for(i=0; i<m; i++)        {            scanf(" (%d %d)",&a,&b);            poi[a][b]++;            poi[b][a]++;        }        for(i=1; i<=n; i++)            if(set[i]==0)            {                set[i]=-1;                Tarjan(i);                break;            }        for(i=1; i<=n; i++)            if(ans[i])                printf("%d:%d\n",i,ans[i]);    }    return 0;}




0 0
原创粉丝点击