并查集
作者:互联网
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int fa[10003]; 5 int n , m; 6 int getf(int p) 7 { 8 if(fa[p] == p) 9 return p; 10 return fa[p] = getf(fa[p]); 11 } 12 int merge(int x , int y) 13 { 14 int t1 = getf(x) , t2 = getf(y); 15 if(t1 == t2) 16 return 0; 17 fa[t1] = t2; 18 return 1; 19 } 20 int main() 21 { 22 int opt , x , y; 23 scanf("%d%d" , &n , &m); 24 for(int i = 1; i <= n; i++) 25 fa[i] = i; 26 for(int i = 1; i <= m; i++) 27 { 28 scanf("%d%d%d" , &opt , &x , &y); 29 if(opt == 1) 30 merge(x , y); 31 if(opt == 2) 32 { 33 if(getf(x) == getf(y)) 34 printf("Y\n"); 35 else 36 printf("N\n"); 37 } 38 } 39 return 0; 40 }
标签:return,int,查集,t2,getf,t1,fa 来源: https://www.cnblogs.com/leo-xy/p/11385911.html