Pagini recente » Cod sursa (job #2263096) | Cod sursa (job #2082633) | Cod sursa (job #2761475) | Cod sursa (job #2071967) | Cod sursa (job #2863687)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
mt19937 G;
uniform_int_distribution<int> ui(1 , 1e9);
int const N = 1e5 + 3;
int n , m;
int idx[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){
a = T(a);
b = T(b);
if(idx[a] < idx[b])
swap(a , b);
t[b] = a;
idx[a] = idx[b];
}
int main()
{
fin >> n >> m;
for(int i = 1 ; i <= n ; ++ i){
t[i] = i;
idx[i] = ui(G);
}
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;
}