Pagini recente » Cod sursa (job #768451) | Cod sursa (job #2619935) | Cod sursa (job #2597736) | Cod sursa (job #79373) | Cod sursa (job #2573162)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
void usain_bolt()
{
ios::sync_with_stdio(false);
fin.tie(0);
}
const int N = 1e5 + 5;
int Size[N], parent[N];
void make_set(int k)
{
parent[k] = k;
Size[k] = 1;
}
int find_set(int k)
{
if(parent[k] == k) return k;
return parent[k] = find_set(parent[k]);
}
void union_sets(int a, int b)
{
a = find_set(a), b = find_set(b);
if(a != b) {
if(Size[b] > Size[a]) swap(a, b);
Size[a] += Size[b];
parent[b] = a;
}
}
int main()
{
usain_bolt();
int n, m;
fin >> n >> m;
for(int i = 1; i <= n; ++i) make_set(i);
for(; m; --m) {
int type, x, y;
fin >> type >> x >> y;
if(type == 1) union_sets(x, y);
else {
fout << ((find_set(x) == find_set(y)) ? "DA" : "NU") << "\n";
}
}
return 0;
}