Pagini recente » Cod sursa (job #2345991) | Cod sursa (job #718273) | Cod sursa (job #2609539) | Cod sursa (job #575777) | Cod sursa (job #2715556)
//infoarena.ro/problema/disjoint
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
const int nMax = 1e5;
int n, m, rnk[nMax], rt[nMax];
void read(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
rnk[i] = 1;
rt[i] = i;
}
}
int root(int nod){
if(rt[nod] != nod)
rt[nod] = root(rt[nod]);
return rt[nod];
}
void uniune(int x, int y){
if(rnk[x] > rnk[y])
rt[y] = x;
else
rt[x] = y;
if(rnk[x] == rnk[y])
rnk[y]++;
}
void operations(){
int cod, x, y;
for(int i = 1; i <= m; i++){
cin >> cod >> x >> y;
if(cod == 1)
uniune(root(x), root(y));
else {
if(root(x) == root(y))
cout << "DA\n";
else
cout << "NU\n";
}
}
}
int main()
{
read();
operations();
return 0;
}