标签:des style blog http color os strong io
传送门:http://poj.org/problem?id=1470
Time Limit: 2000MS | Memory Limit: 10000K | |
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
Source
1 #include<set> 2 #include<queue> 3 #include<vector> 4 #include<cstdio> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 910; 10 #define For(i,n) for(int i=1;i<=n;i++) 11 #define Rep(i,l,r) for(int i=l;i<=r;i++) 12 13 struct edge{ 14 int t,next; 15 }E[N*2]; 16 int head[N]; 17 vector<int> Q[N]; 18 int indeg[N],fa[N],anc[N],Ans[N],Hash[N][N]; 19 int Es,n,m,x,y,tot,root;char ch; 20 bool vis[N]; 21 void makelist(int s,int t){ 22 E[Es].t = t; E[Es].next = head[s]; 23 head[s] = Es++; 24 } 25 26 int read(){ 27 ch = getchar();int num = 0; 28 while(ch>‘9‘||ch<‘0‘) ch = getchar(); 29 while(ch>=‘0‘&&ch<=‘9‘){ 30 num = num * 10 + ch - ‘0‘; 31 ch = getchar(); 32 } 33 return num; 34 } 35 36 int find(int i){ 37 return (fa[i]==i)?(i):(find(fa[i])); 38 } 39 40 void LCA(int i){ 41 anc[i] = i; 42 for(int p = head[i];p!=-1;p=E[p].next){ 43 if(vis[E[p].t]) continue; 44 LCA(E[p].t); 45 fa[find(E[p].t)] = find(i); 46 anc[find(i)] = i; 47 } 48 vis[i] = true; 49 for(int j=0;j<Q[i].size();j++) 50 if(vis[Q[i][j]]&&(Hash[i][Q[i][j]])) { 51 Ans[anc[find(Q[i][j])]]+=Hash[i][Q[i][j]]; 52 Hash[i][Q[i][j]] = Hash[Q[i][j]][i] = 0; 53 } 54 } 55 56 int main(){ 57 while(scanf("%d",&n)!=EOF){ 58 memset(Ans,0,sizeof(Ans));Es = 0; 59 memset(Hash,false,sizeof(Hash)); 60 memset(head,-1,sizeof(head)); 61 memset(indeg,0,sizeof(indeg));memset(anc,0,sizeof(anc)); 62 memset(vis,false,sizeof(vis)); 63 For(i,n){ 64 Q[i].clear(); 65 x = read(); tot = read(); 66 For(j,tot){ 67 y = read(); 68 makelist(x,y); 69 indeg[y]++; 70 } 71 } 72 m = read(); 73 For(i,m){ 74 int x = read() , y = read(); 75 Hash[x][y]++;Hash[y][x]++; 76 Q[x].push_back(y);Q[y].push_back(x); 77 } 78 For(i,n) { 79 root = (!indeg[i]) ? i : root; 80 fa[i] = i; 81 } 82 LCA(root); 83 For(i,n) 84 if(Ans[i]) printf("%d:%d\n",i,Ans[i]); 85 } 86 return 0; 87 }
ClosestCommon Ancestors·POJ1470,布布扣,bubuko.com
ClosestCommon Ancestors·POJ1470
标签:des style blog http color os strong io
原文地址:http://www.cnblogs.com/kjerome/p/3886669.html