Cod sursa(job #3190284)

Utilizator gianiferSpita Alexandru-Mihai gianifer Data 7 ianuarie 2024 14:15:05
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>
#define N_MAX 100005

using namespace std;

ifstream fin("disjoint.in");

ofstream fout("disjoint.out");

int tati[N_MAX], nr[N_MAX];
int n, m;

int getroot(int x)
{
    while (tati[x] != 0)
        x = tati[x];
    return x;
}
void join(int x, int y)
{
    x=getroot(x);
    y=getroot(y);
    if (nr[x] < nr[y])
        swap(x, y);
    tati[y] = x;
}
int main()
{
    int op;
    fin >> n >> m;
    for (; m--;)
    {
        int x, y;
        fin >> op >> x >> y;
        if (op == 1)
            join(x, y);
        else
        {
            if (getroot(x) == getroot(y))
                fout << "DA\n";
            else
                fout << "NU\n";
        }
    }
}