Pagini recente » Cod sursa (job #1377835) | Cod sursa (job #1101132) | Cod sursa (job #1434187) | Cod sursa (job #2976117) | Cod sursa (job #1129710)
#include <fstream>
using namespace std;
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int father[100001],depth[100001];
int n,m,x,y,op;
int parent (int x)
{
if (father[x] != 0)
{
return father[x] = parent(father[x]);
}
return x;
}
void reunion (int x, int y)
{
x = parent(x);
y = parent(y);
if (depth[x] < depth[y])
father[x] = y;
else
{
father[y] = x;
if (depth[x] == depth[y])
depth[x]++;
}
}
int main()
{
fin>>n>>m;
for (int i=1; i<=m; ++i)
{
fin>>op>>x>>y;
if (op == 1)
reunion (x,y);
else
{
if (parent(x) == parent(y))
fout<<"DA";
else fout<<"NU";
fout<<"\n";
}
}
}