Mai intai trebuie sa te autentifici.
Cod sursa(job #2410129)
Utilizator | Data | 19 aprilie 2019 19:25:06 | |
---|---|---|---|
Problema | Paduri de multimi disjuncte | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.77 kb |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int t[MAXN];
int card[MAXN];
int root(int x) {
if(x == t[x]) return x;
return t[x] = root(t[x]);
}
void unite(int x, int y) {
x = root(x), y = root(y);
if(card[x] < card[y]) swap(x, y);
t[y] = x;
card[x] += card[y];
return;
}
int main() {
#ifdef BLAT
freopen("input", "r", stdin);
#else
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; ++i) {
t[i] = i;
card[i] = 1;
}
for(int i = 0; i < m; ++i) {
int t, a, b;
cin >> t >> a >> b;
if(t == 1) {
unite(a, b);
} else {
cout << (root(a)==root(b)?"DA\n":"NU\n");
}
}
return 0;
}