Gidhub BE Developer

BOJ - 집합의 표현

2017-10-17
goodGid

Problem

Problem URL : 집합의 표현

Screenshots of Problem Explain

Code


#include<iostream>

using namespace std;

int p[1000005];

int Find(int x ){
    if ( x == p[x])
        return x;
    else{
        /*
         int y = Find(p[x]);
         p[x] = y;
         return y;
         */
        return p[x] = Find(p[x]);
    }
    return x;
}

void Union(int x, int y){
    x = Find(x);
    y = Find(y);
    p[y] = x;
    
}

int main(){
    for(int i=1; i<=1000005; i++)
        p[i] = i;
    int n,m;
    cin >> n >> m;
    
    int flag, a, b;
    while (m--) {
        scanf("%d%d%d",&flag, &a, &b);
        
        if( flag ){
            // Find
            if( Find(a) == Find(b))
                printf("YES\n");
            else
                printf("NO\n");
        }
        else{
            // Union
            Union(a, b);
        }
    }
    return 0;
}



Feed Back

  • 가벼운 Union & Find 문제 !

Back : Transaction

Next : BinarySearch

Comments

Content