Pagini recente » Cod sursa (job #1089556) | Cod sursa (job #2783520) | Cod sursa (job #2902241) | Cod sursa (job #2265215) | Cod sursa (job #2863681)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int const N = 1e5 + 3;
int n , m;
int p[N] , h[N] , t[N];
int T(int x){
if(t[x] == x)
return x;
return t[x] = T(t[x]);
}
void union_set(int a , int b){
if(t[a] == t[b])
return;
if(h[a] < h[b])
swap(a , b);
t[b] = a;
if(h[b] == h[a])
++ h[a];
}
int main()
{
fin >> n >> m;
for(int i = 1 ; i <= n ; ++ i){
t[i] = i;
h[i] = 1;
p[i] = i;
}
for(int i = 1 ; i <= m ; ++ i){
int type , a , b;
fin >> type >> a >> b;
if(type == 1)
union_set(a , b);
else
fout << (T(a) == T(b) ? "DA\n" : "NU\n");
}
return 0;
}