Pagini recente » Statistici Flirk Navok (stoicatheo) | Cod sursa (job #1570795) | Cod sursa (job #620848) | Cod sursa (job #445712) | Cod sursa (job #2410129)
#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;
}