1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 int f[10000005]; 7 int a, b, c, d; 8 9 inline int find(int x){ 10 if(f[x] != x) 11 f[x] = find(f[x]); 12 return f[x]; 13 } //“查”操作 14 int main(){ 15 int n, m, p; 16 scanf("%d%d%d", &n, &m, &p); 17 for(int i = 1; i <= n; i++) 18 f[i] = i; 19 for(int i = 1; i <= m; i++){ 20 scanf("%d%d", &a, &b); 21 int r1 = find(a); 22 int r2 = find(b); 23 if(r1 != r2) 24 f[r1] = r2; //“并”操作 25 } 26 for(int i = 1; i <= p; i++){ 27 scanf("%d%d", &c, &d); 28 int f1 = find(c); 29 int f2 = find(d); 30 if(f1 == f2) printf("Yes\n"); 31 else printf("No\n"); 32 } 33 return 0; 34 }
原文:https://www.cnblogs.com/New-ljx/p/10800945.html