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