Pagini recente » Cod sursa (job #1917478) | Cod sursa (job #1908441) | Cod sursa (job #1917770) | Cod sursa (job #148554) | Cod sursa (job #2922553)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
const int NM = 1e5 + 5;
int t[NM], c[NM], n, m;
int root (int x){
if (x == t[x]){
return x;
}
return t[x] = root(t[x]);
}
void unite (int x, int y){
x = root(x);
y = root(y);
if (c[x] > c[y]){
swap(x, y);
}
t[y] = x;
c[x] += c[y];
}
int main(){
fin >> n >> m;
for (int i = 1; i <= n; i++){
t[i] = i;
c[i] = 1;
}
while (m--){
int o, x, y; fin >> o >> x >> y;
if (o == 1){
if (root(x) != root(y)){
unite(x, y);
}
}
else{
fout << (root(x) == root(y) ? "DA" : "NU") << '\n';
}
}
}