Cod sursa(job #2576218)

Utilizator flee123Flee Bringa flee123 Data 6 martie 2020 17:54:18
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>
#define nmax 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int n, m;
int t[nmax], height[nmax];

int get_root(int k)
{
    while(t[k] != k)
        k = t[k];
    return k;
}

void unire(int a, int b)
{
    if(height[a] > height[b])
        t[b] = a;
    else if(height[b] > height[a])
        t[a] = b;
    else
        t[b] = a, height[a]++;
}


int main()
{
    int i, op, x, y;
    fin >> n >> m;
    for(i = 1; i <= n; i++)
        t[i] = i;
    for(i = 0; i < m; i++)
    {
        fin >> op >> x >> y;
        if(op == 1)
            x = get_root(x), y = get_root(y), unire(x, y);
        else
         {
             x = get_root(x), y = get_root(y);
             if(x == y)
                fout << "DA" << '\n';
             else
                fout << "NU" << '\n';
         }
    }
    return 0;
}