Pagini recente » Cod sursa (job #3351157) | Cod sursa (job #3357102) | Cod sursa (job #223436) | Cod sursa (job #3355226) | Cod sursa (job #3304117)
#include <bits/stdc++.h>
using namespace std;
int parent[100005], card[100005];
int find(int x) {
if (parent[x] != x)
return find(parent[x]);
return x;
}
void join(int x, int y) {
if (card[x] < card[y])
swap(x, y);
parent[y] = x;
card[x] += card[y];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
parent[i] = i;
card[i] = 1;
}
int type, x, y;
while (m--) {
cin >> type >> x >> y;
if (type == 1) {
join(x, y);
} else {
if (find(x) == find(y))
cout << "DA\n";
else
cout << "NU\n";
}
}
}