Cod sursa(job #3215868)

Utilizator AlexInfoIordachioaiei Alex AlexInfo Data 15 martie 2024 13:53:57
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;

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

#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second

const int NMAX = 1e5 + 30;
const int INF = 0x3f3f3f3f;

int n, m, root[NMAX], op, x, y;

void read()
{
    in >> n>>m;
}

int getroot(int x)
{
    if (root[x]==x)
        return x;
    return root[x] = getroot(root[x]);
}

void solve()
{
    for (int i=1; i<=n; root[i] = i, i++);

    while (m--)
    {
        in>>op>>x>>y;
        x = getroot(x);
        y = getroot(y);
        if (op==1) root[x] = y;
        else {x==y ? out<<"DA"<<'\n' : out<<"NU"<<'\n';}

    }
}

int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);

    read();
    solve();

    return 0;
}