Cod sursa(job #2200703)

Utilizator PushkinPetolea Cosmin Pushkin Data 2 mai 2018 11:36:56
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m, t[100002];
int tata(int x)
{
    if(t[x]!=x)
        t[x]=tata(t[x]);
    return t[x];
}
/*inline void compresie(int x, int r)
{
    int cx;
    while(t[x]!=x)
    {
        cx=t[x];
        t[x]=r;
        x=cx;
    }
}*/
int main()
{
    int op, x, y, r;
    f>>n>>m;
    for(int i=1;i<=n;i++)
        t[i]=i;
    while(f>>op>>x>>y)
    {
        if(op==1)
        {
            x=tata(x);
            y=tata(y);
            t[x]=y;
        }
        else
        {
            r=tata(x);
            //compresie(x, r);
            if(r==tata(y))
                g<<"DA\n";
            else
                g<<"NU\n";
        }
    }
    return 0;
}