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 |
自己画了n个树写进去都过了,就是wa,大佬们帮忙看一下呗#include<cstdio> #include<cstring> using namespace std; const int max=100000+50; int pa[max],gp[max]; void init(int n){ for(int i=1;i<=n;i++){ pa[i]=i;gp[i]=-1; } } int find(int n){ if(n!=pa[n]) pa[n]=find(pa[n]); gp[n]=(gp[n]+gp[pa[n]])%2; return pa[n]; } void unite(int x,int y){ int fx=find(x); int fy=find(y); if(gp[x]==-1) gp[x]=0; if(gp[y]==-1) gp[y]=0; pa[y]=fx; gp[y]=(gp[x]-gp[y]+2+1)%2; } int main(){ int n,a,b; char m; scanf("%d",&n); while(n--){ scanf("%d%d",&a,&b);getchar(); init(a); int x,y; for(int j=0;j<b;j++){ scanf("%c%d%d",&m,&x,&y); getchar(); if(m=='A'&&a==2){ printf("In different gangs.\n"); continue; } if(m=='D') unite(x,y); else if(m=='A'){ if(gp[x]==-1||gp[y]==-1) printf("Not sure yet.\n"); else { if(gp[x]==gp[y]&&find(x)==find(y)) printf("In the same gang.\n"); else if(find(x)==find(y)&&gp[x]!=gp[y])printf("In different gangs.\n"); else printf("Not sure yet.\n"); } } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator