Pagini recente » Cod sursa (job #1862785) | Cod sursa (job #2476235) | Cod sursa (job #7095) | Cod sursa (job #2471295) | Cod sursa (job #2775846)
#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 n)
{
parent[n] = n;
Size[n] = 1;
}
int find_set(int k)
{
if(parent[k] != k) {
return parent[k] = find_set(parent[k]);
}
return 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(int i = 1; i <= m; ++i) {
int type, x, y;
fin >> type >> x >> y;
switch(type) {
case 1:
union_sets(x, y);
break;
case 2:
fout << (find_set(x) == find_set(y) ? "DA" : "NU") << "\n";
break;
}
}
return 0;
}