Cod sursa(job #2340823)

Utilizator NannyiMaslinca Alecsandru Mihai Nannyi Data 11 februarie 2019 09:37:12
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <bits/stdc++.h>
#define nmax 100005
using namespace std;

ifstream f("disjoint.in");
ofstream g("disjoint.out");

int n,m,d[nmax];

int dad(int nod)
{
    if (d[nod]==nod)
        return nod;
    return d[nod]=dad(d[nod]);
}
void unio(int x,int y)
{
    d[dad(x)]=dad(y);
}

int main()
{
    f>>n>>m;
    for (int i=1; i<=n; ++i)
        d[i]=i;
    for (int i=1; i<=m; ++i)
    {
        int op,a,b;
        f>>op>>a>>b;
        if (op==1)
            unio(a,b);
        else
        {
            if (dad(a)==dad(b))
                g<<"DA";
            else
                g<<"NU";
            g<<"\n";
        }
    }
    return 0;
}