Cod sursa(job #2772783)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 2 septembrie 2021 20:36:32
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,t[100005],nivel[100005];

int root(int p)
{
    while (t[p] != 0)
        p = t[p];
    return p;
}

void unite(int x,int y)
{
    if(nivel[x] > nivel[y])
        t[y] = x;
    if(nivel[x] < nivel[y])
        t[x] = y;
    if (nivel[x] == nivel[y])
    {
        t[x] = y;
        nivel[y]++;
    }
}

int main()
{
    in >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int t,x,y;
        in >> t >> x >> y;
        if (t == 1)
            unite(root(x),root(y));
        else
        {
            if (root(x) == root(y))
                out << "DA\n";
            else
                out << "NU\n";
        }
    }
    return 0;
}