Baekjoon #1717

Baekjoon #1717

2개의 풀이 · C++, Python

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
#include<bits/stdc++.h>

using namespace std;

int uf[1000005];

int find(int x) {
    if(uf[x] < 0) return x;
    return uf[x] = find(uf[x]);
}

bool merge(int a, int b) {
    a = find(a); 
    b = find(b);
    if(a == b)return false;
    uf[b] = a;
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N, M; cin >> N >> M;
    for(int i=0;i<=N;i++) uf[i] = -1;
    for(int i=0;i<M;i++) {
        int t, a, b; cin >> t >> a >> b;
        if(t == 1) {
            if(find(a) == find(b)) cout << "YES\n";
            else cout << "NO\n";
        }
        else merge(a, b);
    }

    return 0;
}

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.