Cod sursa(job #2203054)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 10 mai 2018 20:06:43
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 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)
{
    while(tata[x]!=x)
    {
        x=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;
}