Cod sursa(job #2260553)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 15 octombrie 2018 09:49:19
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int n,m,fat[100005];
int tata(int x)
{
    int r=x;
    while(r!=fat[r])
    {
        r=fat[r];
    }
    while(x!=fat[x])
    {
        int c=x;
        x=fat[x];
        fat[c]=r;
    }
    return r;
}
int main()
{
    in >> n >> m;
    for(int i=1; i<=n; i++)
    {
        fat[i]=i;
    }
    for(int i=1; i<=m; i++)
    {
        int x,y,z;
        in >> x >> y >> z;
        int a=tata(y);
        int b=tata(z);
        if(x==1)
        {
            if(a!=b)
                fat[a]=b;
        }
        else
        {
            if(a==b)
                out << "DA\n";
            else
                out << "NU\n";
        }
    }
    return 0;
}