Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
改了一天,终于AC希望能给WA的同学启发。 /* * poj- Find them, Catch them * mike-w * 2011-10-26 * --------------------------- * url: http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1003&ojid=1&cid=790&hide=0 * --------------------------- * 并查集 * --------------------------- * 改了一天,总算AC了!!! */ #include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE 100100 long set[SIZE],rec[SIZE],st[SIZE]; long N,M,T; int init(long size) { long i; for(i=0;i<=size;i++) set[i]=-1,rec[i]=0; return 0; } #ifndef recursive_find long find(long e) { long top=0,node=e,root; while(set[node]>0) { st[top++]=node; node=set[node]; } root=node; while(top--) { node=st[top]; rec[node]=(rec[node]+rec[set[node]])&0x1; set[node]=root; } return root; } #endif #ifdef recursive_find long find(long e) { if(set[e]<0) return e; long node=set[e]; set[e]=find(set[e]); rec[e]=(rec[e]+rec[node])&0x1; return set[e]; } #endif long merge(long e1,long e2) { long r1=find(e1); long r2=find(e2); if(r1==r2) return 0; long flag=!((rec[e1]+rec[e2])&0x1); if(set[r1]>set[r2]) /* attach r1 to r2 */ rec[r1]=flag,set[r1]=r2; else if(set[r1]<set[r2]) /* attach r2 to r1 */ rec[r2]=flag,set[r2]=r1; else rec[r1]=flag,set[r1]=r2,set[r2]--; return 1; } int main(void) { long i,t1,t2,r1,r2; char buf[10]; #ifndef ONLINE_JUDGE freopen("in","r",stdin); #endif scanf("%ld",&T); while(T-->0) { scanf("%ld%ld",&N,&M); init(N); for(i=0;i<M;i++) { scanf("%s%ld%ld",buf,&t1,&t2); if(buf[0]=='D') merge(t1,t2); else { r1=find(t1); r2=find(t2); if(r1!=r2) puts("Not sure yet."); else { if(rec[t1]==rec[t2]) puts("In the same gang."); else puts("In different gangs."); } } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator