Cod sursa(job #2474488)

Utilizator cpopescu1Popescu Cristian cpopescu1 Data 15 octombrie 2019 12:15:34
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include<bits/stdc++.h>
#define maxn 100006
using namespace std;

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

int n,m,tata[maxn];


int BIG_DADDY(int nod)
{
    int R=nod;
    while(tata[R]!=0)
        R=tata[R];
    while(tata[nod]!=0)
    {
        int aux=tata[nod];
        R=tata[nod];
        nod=aux;
    }
    return R;
}


void unim(int A,int B)
{
    A=BIG_DADDY(A);
    B=BIG_DADDY(B);
    tata[A]=B;
}


int main()
{
    fin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,cod;
        fin>>cod>>x>>y;
        if(cod==1)
            unim(x,y);
        else
        {
            if(BIG_DADDY(x)==BIG_DADDY(y))
            {
                fout<<"DA\n";
            }
            else
                fout<<"NU\n";
        }
    }
    return 0;
}