Cod sursa(job #2869786)

Utilizator Sebi_MafteiMaftei Sebastioan Sebi_Maftei Data 11 martie 2022 20:29:20
Problema Paduri de multimi disjuncte Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("disjoint.in");
ofstream fout("disjoint.out");

int n, m, t[100003];

void Union(int x, int y)
{
    t[x] = y;
}

int Find(int x)
{
    int rad, y;
    rad = x;
    while(t[rad] != 0)
        rad = t[rad];
    while (x != rad)
    {
        y = t[x];
        t[x] = rad;
        x = y;
    }
    return rad;
}

int main()
{
    int x, y, op;
    fin >> n >> m;
    while (m--)
    {
        fin >> op >> x >> y;
        if (op == 1 && Find(x) != Find(y)) Union(x, y);
        else if (Find(x) == Find(y)) fout << "DA\n";
        else fout << "NU\n";
    }
    return 0;
}