Cod sursa(job #1168027)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 6 aprilie 2014 18:00:58
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include<cstdio>

using namespace std;

const int NMAX = 100000+5;

int N,M;
int Root[NMAX];
int Rang[NMAX];

void Read(),Solve();

int Find(int x)
{
    if(Root[x] != x) Root[x] = Find(Root[x]);
    return Root[x];
}

void Union(int x,int y)
{
    int tx = Find(x),ty = Find(y);
    if(Rang[tx] < Rang[ty])
    {
        Root[x] = ty;
        return;
    }
    if(Rang[tx] > Rang[ty])
    {
        Root[y] = tx;
        return;
    }
    Root[x] = ty;
    Rang[tx]++;
}

int main()
{
    Read();
    Solve();

    return 0;
}

void Read()
{
    freopen("disjoint.in","r",stdin);
    freopen("disjoint.out","w",stdout);

    scanf("%d%d",&N,&M);
}

void Solve()
{
    int t,x,y;

    for(; N; --N)
        Root[N] = N;

    for(; M; --M)
    {
        scanf("%d%d%d",&t,&x,&y);
        if(t == 1) Union(x,y);
        else printf("%s\n",Find(x) == Find(y)?"DA":"NU");
    }
}