Pagini recente » Cod sursa (job #53888) | Cod sursa (job #2414000) | Cod sursa (job #912204) | Cod sursa (job #1871518) | Cod sursa (job #2568735)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int DIM = 1e5 + 1;
int siz[DIM];
int to[DIM];
int find(int nod)
{
if(to[nod] == nod)
return nod;
return (to[nod] = find(to[nod]));
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
if(siz[x] < siz[y])
swap(x, y);
to[y] = x;
siz[x] += siz[y];
}
main()
{
int n, m;
fin >> n >> m;
for(int i = 1; i <= n; ++i)
{
to[i] = i;
siz[i] = 1;
}
for(; m; --m)
{
int op, x, y;
fin >> op >> x >> y;
if(op == 1)
{
unite(x, y);
}
else
{
if(find(x) != find(y))
{
fout << "NU\n";
}
else
{
fout << "DA\n";
}
}
}
}