Cod sursa(job #2694106)

Utilizator SirbuSirbu Ioan Sirbu Data 8 ianuarie 2021 00:41:14
Problema Paduri de multimi disjuncte Scor 100
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");

vector<int> t;

int find(int x) {
	if(t[x] == 0)
		return x;

	t[x] = find(t[x]);
	return t[x];
}



void unite(int x, int y) {
	int rx = find(x);
	int ry = find(y);
	t[rx] = ry;
}

int main(){

    int n, q;
    fin >> n >> q;
    t.resize(n + 1);

    while(q--) {
        int t, x, y;
        fin >> t >> x >> y;
        if(t == 1)
            unite(x, y);
        else {
            if(find(x) == find(y))
                fout << "DA\n";
            else
                fout << "NU\n";
        }
    }
    return 0;
}