Description
Input
Output
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
#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 采用树结构的非线性表编程
原文地址:http://blog.csdn.net/zp___waj/article/details/47982401