Cod sursa(job #1581747)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 27 ianuarie 2016 09:13:36
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.63 kb
#include <cstdio>

#define NMax 1000053
using namespace std;

int root[NMax];
int x,y,n,m,p;
int main()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= m; ++i){
        scanf("%d%d%d",&p,&x,&y);
        int w = y;
        while(root[w] != 0)
            w = root[w];
        int t = x;
        while(root[t] != 0)
            t = root[t];
        if(p == 1){
            root[t] = w;
        }else{
            if(t != w){
                printf("NU\n");
            }else
                printf("DA\n");
        }
    }
    return 0;
}