P1551 친척(및 세트 항목 질문 확인) C++


주제 설명

링크
여기에 이미지 설명 삽입

아이디어

노동 조합 템플릿 참고 사항 참조

코드 1

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;//数据太大 要开ll
const int N = 100010;
int p[N];

int find(int x) {
    
    
    if(p[x] != x) {
    
    
        p[x] = find(p[x]);
    }
    return p[x];
}

int main () {
    
    
    int n, m, x;
    cin >> n >> m >> x;
    for (int i = 1; i <= n; i ++) {
    
    
        p[i] = i;
    }
    while (m --) {
    
    
        int a, b;
        cin >> a >> b;
        p[find(a)] = find(b);
    }
    while (x --) {
    
    
        int a, b;
        cin >> a >> b;
        if(find(a) == find (b)) {
    
    
            puts("Yes");
        } else {
    
    
            puts("No");
        }
    }
     return 0;
}

추천

출처blog.csdn.net/weixin_49486457/article/details/123972888