Pagini recente » Borderou de evaluare (job #1036385) | Clasament preONI 2007, Runda 2, Clasa a 9-a si gimnaziu | Cod sursa (job #1187745) | Cod sursa (job #2687569) | Cod sursa (job #2456469)
#include <bits/stdc++.h>
using namespace std;
int P[100005];
int h[100005];
int root(int x) {
if (P[x] == x) return x;
return root(P[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (h[rx] > h[ry]) {
P[ry] = rx;
h[rx]++;
} else {
P[rx] = ry;
h[ry]++;
}
}
int main() {
ios_base::sync_with_stdio(false);
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) P[i] = i, h[i] = 1;
while (m--) {
int t, x, y;
cin >> t >> x >> y;
if (t == 1) {
unite(x, y);
} else {
cout << (root(x) == root(y) ? "DA\n" : "NU\n");
}
}
}