Cod sursa(job #2203061)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 10 mai 2018 20:11:43
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
#define N_MAX 100005
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int tata[N_MAX],n,m;
int father(int x)
{
    if(tata[x]!=x)
    {
        tata[x]=father(tata[x]);
    }
    return tata[x];
}
int main()
{
    in >> n >> m;
    for(int i=1; i<=n; i++)
    {
        tata[i]=i;
    }
    for(int i=0; i<m; i++)
    {
        int x,y,z;
        in >> x >> y >> z;
        if(x==1)
        {
            y=father(y);
            z=father(z);
            tata[y]=z;
        }
        else
        {
            int r=father(y);
            if(r==father(z))
                out << "DA\n";
            else
                out << "NU\n";
        }
    }
    return 0;
}