标签:代码 display 并查集 bsp color tree 通过 rip 等等
Description
Input
Output
Sample Input
Sample Output
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int MAX=1e5+10; 6 int pre[MAX]; 7 int vis[MAX]; 8 int flag; 9 void init() 10 { 11 int i; 12 for(i=1; i<MAX; i++) 13 { 14 vis[i]=0; 15 pre[i]=i; 16 } 17 } 18 int Find(int x) 19 { 20 if(pre[x]==x) 21 { 22 return x; 23 } 24 else 25 { 26 return Find(pre[x]); 27 } 28 } 29 void Union(int root1,int root2) 30 { 31 int x,y; 32 x=Find(root1); 33 y=Find(root2); 34 if(x!=y) 35 { 36 pre[x]=y; 37 } 38 else///判环 39 { 40 flag=0; 41 } 42 } 43 int main() 44 { 45 int i,root,counts,a,b; 46 while(scanf("%d%d",&a,&b)!=EOF) 47 { 48 if(a==-1&&b==-1) 49 { 50 break; 51 } 52 if(a==0&&b==0) 53 { 54 printf("Yes\n"); 55 continue; 56 } 57 flag=1; 58 counts=0; 59 init(); 60 vis[a]=1; 61 vis[b]=1; 62 Union(a,b); 63 while(scanf("%d%d",&a,&b)!=EOF) 64 { 65 if(a==0&&b==0) 66 { 67 break; 68 } 69 vis[a]=1; 70 vis[b]=1; 71 Union(a,b); 72 } 73 for(i=1; i<MAX; i++) 74 { 75 if(vis[i]&&pre[i]==i) 76 { 77 counts++; 78 } 79 } 80 if(counts>1) 81 { 82 flag=0; 83 } 84 if(flag) 85 { 86 printf("Yes\n"); 87 } 88 else 89 { 90 printf("No\n"); 91 } 92 } 93 return 0; 94 }
标签:代码 display 并查集 bsp color tree 通过 rip 等等
原文地址:https://www.cnblogs.com/wkfvawl/p/9671317.html