Cod sursa(job #1011853)

Utilizator DaniEsDani Stetcu DaniEs Data 17 octombrie 2013 17:15:34
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#define NMax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int TT[NMax],N,M;
int Father(int x)
{
    while(TT[x]!=x)
        x=TT[x];
    return x;
}
void Unite(int x, int y)
{
    TT[x]=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;
}