Pagini recente » Cod sursa (job #2092798) | Cod sursa (job #3145055) | Cod sursa (job #1642338) | Cod sursa (job #2101481) | Cod sursa (job #1824164)
#include <iostream>
#include <stdio.h>
using namespace std;
int N, M;
int r[100001];
int dim[100001];
void init(){
for(int i=1; i<=N; i++){
r[i] = i;
dim[i] = 1;
}
}
int gaseste_radacina(int x){
while(r[x] != x){
x = r[x];
}
return x;
}
void join(int x, int y){
int i, j;
dim[x] > dim[y] ? i=x, j=y: i=y; j=x;
r[j] = gaseste_radacina(i);
if(dim[i]>dim[j]){
r[j] = r[i];
}
else{
dim[i]++;
}
}
void aceeasi_multime(int x, int y){
if(gaseste_radacina(x) == gaseste_radacina(y))
cout << "DA\n";
else
cout << "NU\n";
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
int op, x, y;
cin >> N >> M;
init();
for(int i=0; i<M; i++){
cin >> op >> x >> y;
if(op == 1)
join(x, y);
else
aceeasi_multime(x, y);
}
return 0;
}