Pagini recente » Cod sursa (job #1974207) | Cod sursa (job #344505) | Cod sursa (job #3143093) | Cod sursa (job #2607565) | Cod sursa (job #2347400)
#include <fstream>
#define N 100000
using namespace std;
ifstream cin ("disjoint.in");
ofstream cout ("disjoint.out");
int tata[N + 1];
int cnt[N + 1];
int rad(int x){
if (tata[x] == 0) return x;
else tata[x] = rad(tata[x]);
return tata[x];
}
bool query(int x, int y){
return (rad(x) == rad(y));
}
void merge(int x, int y){
x = rad(x);
y = rad(y);
if (x == y) return ;
if (cnt[x] > cnt[y]) swap(x, y);
tata[x] = y;
cnt[y] += cnt[x];
}
int main(){
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i++){
tata[i] = 0;
cnt[i] = 1;
}
for(int i = 1; i <= m; i++){
int op, x, y; cin >> op >> x >> y;
if (op == 1) merge(x, y);
else
if (query(x, y)) cout << "DA\n";
else cout << "NU\n";
}
return 0;
}