6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0 -1 -1
Yes Yes No这道题我是要死的节奏。f[f2]=f1;就A掉了。f[f1]=f2;他就挂了我的。。。。AC代码:#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<queue> #include<sstream> #include<cmath> using namespace std; #define M 100500 int f[M],tot,cur; int find (int x) { return f[x] == x ? x : f[x] = find(f[x]); } void make(int a,int b) { int f1=find(a); int f2=find(b); if(f1==f2) tot=1; else f[f2]=f1; } int main() { int a,b; while(1) { memset(f,0,sizeof(f)); tot=0;cur=0; while(scanf("%d%d",&a,&b)!=EOF&&a&&b) { if(a==-1&&b==-1) return 0; if(f[a]==0) f[a]=a; if(f[b]==0) f[b]=b; make(a,b); } for(int i=1;i<M;i++) if(f[i]==i) cur++; if(cur>1||tot==1) printf("No\n"); else printf("Yes\n"); } return 0; }
原文地址:http://blog.csdn.net/qq2256420822/article/details/37769881