Cod sursa(job #796644)
#include <fstream>
using namespace std;
int tata[100010], h[100010];
inline int find(int x)
{
int y, r;
r = x;
while (r!=tata[r])
r = tata[r];
while (x!=tata[x])
{
y = tata[x];
tata[x] = r;
x = y;
}
return r;
}
inline void unite(int x, int y)
{
if (h[x] > h[y])
tata[y] = x;
else
tata[x] = y;
if (h[x] == h[y])
h[y]++;
}
int main()
{
int n, m, cod, x, y, i;
ifstream f("disjoint.in");
f>>n>>m;
for (i=1; i<=n; i++)
{
tata[i] = i;
h[i] = 1;
}
ofstream g("disjoint.out");
for (i=1; i<=m; i++)
{
f>>cod>>x>>y;
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;
}