Pagini recente » Cod sursa (job #2803182) | Cod sursa (job #2480928) | Cod sursa (job #541032) | Cod sursa (job #634847) | Cod sursa (job #2093703)
/// disjoint set of trees
#include <iostream>
#include <fstream>
#include <algorithm>
#include <utility>
#define NMax 100001
///#define f cin
///#define g cout
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, m, root[NMax], rang[NMax], cod, x, y;
int findroot(int x)
{
while(x != root[x])
x = root[x];
return x;
}
int main()
{
f >> n >> m;
for(int i = 1; i <= n; ++i)
{
rang[i] = 1;
root[i] = i;
}
for(int i = 1; i <= m; ++i)
{
f >> cod >> x >> y;
if(cod == 1)
{
int rootx = findroot(x);
int rooty = findroot(y);
if(rang[rootx] > rang[rooty])
{
rang[rootx] += rang[rooty];
root[rooty] = rootx;
}
else
{
rang[rooty] += rang[rootx];
root[rootx] = rooty;
}
}
else
{
int rootx = findroot(x);
int rooty = findroot(y);
if(rootx == rooty) g << "DA\n";
else g << "NU\n";
}
}
return 0;
}