Cod sursa(job #2372543)

Utilizator butasebiButa Gabriel-Sebastian butasebi Data 7 martie 2019 09:46:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
using namespace std;
int a, b, c, n, m, i, t[100005];
int dad(int x)
{
    int y = x;
    while(t[x] != x)x = t[x];
    while(x != y)
    {
        int aux = t[y];
        t[y] = x;
        y = aux;
    }
    return x;
}
int main()
{
    ifstream f("disjoint.in");
    ofstream g("disjoint.out");
    f >> n >> m;
    for(i = 1; i <= n; i ++)
        t[i] = i;
    for(i = 1; i <= m; i ++)
    {
        f >> c >> a >> b;
        if(c == 1)t[dad(a)] = dad(b);
        else
        {
            if(dad(a) == dad(b))g << "DA" << "\n";
            else g << "NU" << "\n";
        }
    }
    return 0;
}