Cod sursa(job #796652)
#include <fstream>
#include <iostream>
using namespace std;
int comp[100010];
inline int find(int x)
{
int r, y;
r = x;
while (comp[r] > 0)
r = comp[r];
while (comp[x] > 0)
{
y = comp[x];
comp[x] = r;
x = y;
}
return r;
}
inline void unite(int x, int y)
{
if (comp[x] > comp[y])
{
comp[y] += comp[x];
comp[x] = y;
}
else
{
comp[x] += comp[y];
comp[y] = x;
}
}
int main()
{
int n, m, x, y, cod, i;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
f>>n>>m;
for (i=1; i<=n; i++)
comp[i] = -1;
for (i=1; i<=m; i++)
{
f>>cod>>x>>y;
//cout<<cod<<" "<<x<<" "<<y<<"\n";
if (cod == 2)
{
if (find(x) == find(y))
g<<"DA\n";
else
g<<"NU\n";
}
else
unite(find(x), find(y));
}
f.close();
g.close();
return 0;
}