Cod sursa(job #2715710)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 4 martie 2021 09:05:26
Problema Paduri de multimi disjuncte Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;

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

int card[100005];
int root[100005];
int n,m;
int c,x,y;
int a,b;

void read() {
    f >> n >> m;
    for (int i=1;i<=n;i++) {
        card[i] = 1;
        root[i] = i;
    }
}

int find_root(int nod) {
    if (nod != root[nod]) {
        root[nod] = find_root(root[nod]);
        return root[nod];
    }
    return nod;
}

void reunion() {
    a = find_root(x);
    b = find_root(y);
    if (card[a]<card[b]) {
        swap(a,b);
    }
    root[b] = a;
    card[a] += card[b];
}

bool check_set() {
    a = find_root(x);
    b = find_root(y);
    return a == b;
}

int main()
{
    read();
    for (int i=1;i<=m;i++) {
        f >> c >> x >> y;
        if (c==1) {
            reunion();
        }
        else {
            if (check_set()==1) {
                g << "DA";
            }
            else {
                g << "NU";
            }
            g << '\n';
        }
    }
    return 0;
}