Pagini recente » Cod sursa (job #2693599) | Cod sursa (job #1440980) | Cod sursa (job #11452) | Cod sursa (job #24328) | Cod sursa (job #2526145)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int maxn = 1e5 + 5;
int t[maxn], h[maxn];
int Find(int x) {
int r = x;
while(t[r]) {
r = t[r];
}
while(t[x]) {
int y = t[x];
t[x] = r;
x = y;
}
return r;
}
void Union(int x, int y) {
if(h[x] > h[y]) {
t[y] = x;
}
else {
t[x] = y;
if(h[x] == h[y]) ++h[y];
}
}
int main() {
int n, m;
fin >> n >> m;
while(m--) {
int k, x, y;
fin >> k >> x >> y;
if(k == 1) Union(Find(x), Find(y));
else fout << (Find(x) == Find(y) ? "DA\n" : "NU\n");
}
return 0;
}