Cod sursa(job #1865817)

Utilizator GoogalAbabei Daniel Googal Data 2 februarie 2017 09:42:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <fstream>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int n,m,x,y,p,t[100001];

int father(int k)
{
    if(k==t[k])
        return k;
    return father(t[k]);
}

int main()
{
    int i;
    fin>>n>>m;
    for(i=1;i<=n;i++)
        t[i]=i;
    for(i=1;i<=m;i++)
    {
        fin>>p>>x>>y;
        x=father(x);
        y=father(y);
        if(p==1)
        {
            t[y]=x;
        }
        else
        {
            if(x!=y)
                fout<<"NU\n";
            else fout<<"DA\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}