码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1470 Closest Common Ancestors 采用树结构的非线性表编程

时间:2015-08-25 21:48:27      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:c++   线性表   数据结构   poj   并查集   

A - Closest Common Ancestors(8.4.9)
Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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 descri_ption, 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 descri_ption 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.
accode
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
#define maxn 900+5
using namespace std;
vector<int>tree[maxn];
int q[maxn][maxn],flag[maxn];
int ans[maxn],set[maxn],n;
int set_find(int x){
    if(set[x]<0)return x;
    return set[x]=set_find(set[x]);
}
void tarjan(int x){
    int i;
    for(i=0;i<tree[x].size();++i){
        tarjan(tree[x][i]);
        set[tree[x][i]]=x;
    }
    flag[x]=true;
    for(i=1;i<=n;++i){
        if(flag[i]&&q[x][i])
            ans[set_find(i)]+=q[x][i];
    }
}
int main(){
    int t,num,x,y,i;
    while(~scanf("%d",&n)){
        for(i=1; i<=n; i++)
            tree[i].clear();
        memset(flag,0,sizeof(flag));
        memset(q,0,sizeof(q));
        memset(ans,0,sizeof(ans));
        memset(set,0,sizeof(set));
        for(i=0;i<n;++i){
            scanf("%d:(%d)",&t,&num);
            while(num--){
                scanf("%d",&x);
                tree[t].push_back(x);
                set[x]=-1;
            }
        }
        scanf("%d",&num);
        for(i=0;i<num;++i){
            scanf(" (%d %d)",&x,&y);
            q[x][y]++;
            q[y][x]++;
        }
        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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1470 Closest Common Ancestors 采用树结构的非线性表编程

标签:c++   线性表   数据结构   poj   并查集   

原文地址:http://blog.csdn.net/zp___waj/article/details/47982401

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!