Cod sursa(job #2340821)

Utilizator NannyiMaslinca Alecsandru Mihai Nannyi Data 11 februarie 2019 09:34:01
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 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[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;
}