Cod sursa(job #2986859)

Utilizator PingStrpewpewpac PingStr Data 1 martie 2023 13:34:21
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
using namespace std;


ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int N = 100002, M = 100002;

int n, m, arb[N], c, x, y;

int find(int a)
{
    if (arb[a] == a)
        return a;
    return arb[a] = find(arb[a]);
}


void update(int a, int b)
{
    int x = find(a), y = find(b);
    arb[x] = y;
}

int main()
{
    fin>>n>>m;
    for (int i = 1; i <= n; i++)
        arb[i] = i;
    for (int i = 1; i <= m; i++)
    {
        fin>>c>>x>>y;
        if(c == 1)
        {
            update(x, y);
        }
        else
        {
            if (find(x) == find(y))
                fout<<"DA"<<"\n";
            else
                fout<<"NU"<<"\n";
        }
    }
}