Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 11754 | Accepted: 3875 |
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
RE代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define N 50010 int n,m,q,e,eq; int num[N]; int first[N],next[N],v[N],w[N]; int first_q[N],next_q[N],u_q[N],v_q[N],lca[N]; int d[N],p[N]; int flag[N]; bool ok[N]; void init() { e=eq=0; memset(first,-1,sizeof(first)); memset(first_q,-1,sizeof(first_q)); memset(lca,0,sizeof(lca)); memset(p,-1,sizeof(p)); memset(ok,0,sizeof(ok)); } void add(int a,int b) { v[e]=b; next[e]=first[a]; first[a]=e++; } void add_q(int a,int b) { u_q[eq]=a; v_q[eq]=b; next_q[eq]=first_q[a]; first_q[a]=eq++; } void make_set(int i) { p[i]=i; } int find_set(int i) { if(i^p[i]) p[i]=find_set(p[i]); return p[i]; } void union_set(int i,int j) { i=find_set(i),j=find_set(j); p[j]=i; } void dfs(int a) { int i,b; make_set(a); for(i=first[a];i!=-1;i=next[i]) { b=v[i]; if(p[b]==-1) { dfs(b); p[b]=a; } } for(i=first_q[a];i!=-1;i=next_q[i]) if(!ok[i]) { b=v_q[i]; if(p[b]!=-1)lca[i]=find_set(b),ok[i]=true; } } inline bool get(int &a) { char c; while(((c=getchar())<'0'||c>'9')&&c!=EOF); if(c==EOF)return 0; for(a=0;c>='0'&&c<='9';c=getchar())a=a*10+c-'0'; return 1; } int main() { int a,b,c,m,i; char s1,s2,s3; while(get(n)) { init(); memset(flag,-1,sizeof(flag)); memset(num,0,sizeof(num)); for(i=0;i<n;i++) { get(a);get(m); while(m--) { get(b); add(a,b); flag[b]++; } } get(q); while(q--) { get(a);get(b); add_q(a,b); add_q(b,a); } for(i=1;i<=n;i++) if(flag[i]==-1) { dfs(i); } for(int i=0;i<eq;i+=2) { a=u_q[i]; b=v_q[i]; c=lca[i]; if(!c) c=lca[i+1]; num[c]++; } for(i=1;i<=n;i++) if(num[i])printf("%d:%d\n",i,num[i]/2); } return 0; }