Cod sursa(job #1818123)

Utilizator paulstepanovStepanov Paul paulstepanov Data 28 noiembrie 2016 20:43:29
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

const int Nmax=100005;
int V[Nmax],TT[Nmax];
int N,M;

int Father(int Nod)
{
    while(Nod!=TT[Nod])
        Nod=TT[Nod];
    return Nod;
}

void Unite(int x,int y)
{
    if(V[x] > V[y])
    TT[y] = x;
    if(V[y] > V[x])
    TT[x] = y;
    if(V[x] == V[y])
    {
      TT[x] = y;
      V[y]++;
    }
}

void ReadandPrint()
{
    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));
        if(op==2)
        {
            if(Father(x)==Father(y)) fout<<"DA\n";
            else fout<<"NU\n";
        }
    }

}

int main()
{
    ReadandPrint();
}