Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14162 Accepted Submission(s): 4297
#include <stdio.h> #include <string.h> bool f[100005]; //标记节点是否已经存在 int g[100005]; //并查集开辟的数组 int Find(int x) //查集合中的末尾元素 { if(g[x]!=x) g[x]=Find(g[x]); return g[x]; } int main() { int x,y,i; int a,b,flag; int dian,bian; while(scanf("%d%d",&x,&y)==2&&(x+y!=-2)) { if (x==0 && y==0) //卧槽 ,少加个这样的条件一直没过,然后放弃了........(铭记教训)血的教训 { printf ("Yes\n"); continue; } memset(f,false,sizeof(f)); //初始化标记数组 f[x]=true; f[y]=true; dian=2; bian=1; flag=1; for(i=1;i<=100005;i++) g[i]=i; g[x]=y; while(scanf("%d%d",&x,&y)==2&&(x+y)) { if(!f[x]) { dian++; f[x]=true; } if(!f[y]) { dian++; f[y]=true; } bian++; //以上记录图中点和边的数量 a=Find(x); b=Find(y); if(a!=b) g[a]=b; else flag=0; // 以上合并新节点,如果有环则标记 } if(bian==dian-1&&flag) printf("Yes\n"); else printf("No\n"); } return 0; }