Pagini recente » Cod sursa (job #2888780) | Cod sursa (job #2712053) | Cod sursa (job #2447453) | Cod sursa (job #2720634) | Cod sursa (job #2715710)
#include <bits/stdc++.h>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int card[100005];
int root[100005];
int n,m;
int c,x,y;
int a,b;
void read() {
f >> n >> m;
for (int i=1;i<=n;i++) {
card[i] = 1;
root[i] = i;
}
}
int find_root(int nod) {
if (nod != root[nod]) {
root[nod] = find_root(root[nod]);
return root[nod];
}
return nod;
}
void reunion() {
a = find_root(x);
b = find_root(y);
if (card[a]<card[b]) {
swap(a,b);
}
root[b] = a;
card[a] += card[b];
}
bool check_set() {
a = find_root(x);
b = find_root(y);
return a == b;
}
int main()
{
read();
for (int i=1;i<=m;i++) {
f >> c >> x >> y;
if (c==1) {
reunion();
}
else {
if (check_set()==1) {
g << "DA";
}
else {
g << "NU";
}
g << '\n';
}
}
return 0;
}