Pagini recente » Cod sursa (job #421079) | Cod sursa (job #343775) | Cod sursa (job #1143133) | Cod sursa (job #690046) | Cod sursa (job #2289144)
#include <iostream>
#include <fstream>
#define NMAX 100010
using namespace std;
int father[NMAX], height[NMAX];
int findFather(int k)
{
int f, aux;
for (f=k; f!=father[f]; f=father[f])
{}
for (; father[k]!=k;)
{
aux = father[k];
father[k] = f;
k = aux;
}
return f;
}
void unite(int x, int y)
{
if (height[x] < height[y])
father[x] = y;
else
father[y] = x;
if (height[x] == height[y])
height[y]++;
}
int main()
{
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int n, m, k, x, y, value;
fin >> n >> m;
for (int i=1; i<=n; i++)
{
father[i] = i;
height[i] = 1;
}
for (int i=1; i<=m; i++)
{
fin >> k >> x >> y;
if (k == 1)
{
if (findFather(x) == findFather(y))
{
return 0;
}
unite(x, y);
}
else
{
if (findFather(x) == findFather(y))
fout << "DA\n";
else
fout << "NU\n";
}
}
return 0;
}