Pagini recente » Cod sursa (job #2293066) | Cod sursa (job #750528) | Cod sursa (job #710820) | Cod sursa (job #2585780) | Cod sursa (job #2277291)
#include <iostream>
#include <fstream>
using namespace std;
#define NMAX 100001
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int tata[NMAX], inaltime[NMAX];
int verfrad( int x)
{
int rad = x, aux;
while (tata[rad] != rad)
rad = tata[rad];
while (tata[x] != x)
{
aux = tata[x];
tata[x] = rad;
x = aux;
}
return rad;
}
void uneste(int x, int y)
{
if(inaltime[x] > inaltime[y])
tata[y] = x;
else
tata[x] = y;
if(inaltime[x] == inaltime[y])
inaltime[y]++;
}
int main()
{
int k, n, m, i, x, y;
f >> n >> m;
for (i = 1; i <= n; i++)
{
tata[i] = i;
inaltime[i] = 1;
}
for (i = 1; i <= m; i++)
{
f >> k >> x >> y;
if (k == 2)
{
if ( verfrad(x) == verfrad(y))
g << "DA" << "\n";
else
g << "NU" << "\n";
}
else
uneste(verfrad(x), verfrad(y));
}
return 0;
}