Pagini recente » fmi-no-stress-2012/solutii/berarii2 | Cod sursa (job #2673587) | Cod sursa (job #420136) | Cod sursa (job #2498531) | Cod sursa (job #2227187)
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0);
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int N = 1e5 + 7;
int TO[N];
int R[N];
int find(int nod)
{
int i;
for(i = nod; TO[i] != i; i = TO[i]);
for(; TO[nod] != nod;)
{
int y = TO[nod];
TO[nod] = i;
nod = y;
}
return i;
}
void unite(int x, int y)
{
if(R[x] > R[y])
TO[y] = x;
else
TO[x] = y;
if(R[x] == R[y])
R[y]++;
}
main()
{
IOS
int n, m;
in >> n >> m;
for(int i = 1; i <= n; i++)
{
TO[i] = i;
R[i] = 1;
}
for(int i = 1; i <= m; i++)
{
int op, x, y;
in >> op >> x >> y;
if(op == 1)
unite(find(x), find(y));
else
out << ((find(x) == find(y)) ? "DA\n" : "NU\n");
}
}