Cod sursa(job #1011855)

Utilizator DaniEsDani Stetcu DaniEs Data 17 octombrie 2013 17:23:49
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#define NMax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int TT[NMax],R[NMax],N,M;
int Father(int x)
{
    while(TT[x]!=x)
        x=TT[x];
    return x;
}
void Unite(int x, int y)
{
    if(R[x]<R[y])
        TT[x]=y;
    if(R[x]>R[y])
        TT[y]=x;
    if(R[x]==R[y])
    {
        TT[x]=y;
        R[y]++;
    }
}
int main()
{
    fin>>N>>M;
    for(int i=1;i<=N;i++)
        TT[i]=i;
    while(M--)
    {
        int op,x,y;
        fin>>op>>x>>y;
        if(op==1)
            Unite(Father(x),Father(y));
        else
            if(Father(x)==Father(y))
                fout<<"DA\n";
            else
                fout<<"NU\n";
    }
    return 0;
}