Cod sursa(job #2986857)

Utilizator PingStrpewpewpac PingStr Data 1 martie 2023 13:32:01
Problema Paduri de multimi disjuncte Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 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 find(arb[a]);
}


void update(int a, int b)
{
    int x = find(a), y = find(b);
    if (x != y)
        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";
        }
    }
}