Pagini recente » Monitorul de evaluare | Cod sursa (job #2450637) | Cod sursa (job #2043262) | Diferente pentru schimbare-borland/alternativa intre reviziile 1 si 14 | Cod sursa (job #2427413)
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 100100
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
vector <pair <int, int> > graf[NMAX];
int grad[NMAX];
int tata[NMAX];
int n, m;
int gasesteTata(int nod)
{
if (tata[nod] == nod) return nod;
tata[nod] = gasesteTata(tata[nod]);
return tata[nod];
}
void combina(int x, int y)
{
int tx, ty;
tx = gasesteTata(x);
ty = gasesteTata(y);
if (tx != ty)
{
if (grad[tx] >= grad[ty])
{
tata[ty] = tx;
grad[tx] = grad[tx] + grad[ty];
}
else
{
tata[tx] = ty;
grad[ty] = grad[ty] + grad[tx];
}
}
}
int main()
{
int i, a, b, c;
f >> n >> m;
for (i = 1; i <= n; i++)
{
tata[i] = i;
grad[i] = 0;
}
for (i = 1; i <= m; i++)
{
f >> a >> b >> c;
if (a == 1)
combina(b, c);
else
if (gasesteTata(b) == gasesteTata(c))
g << "DA" << "\n";
else
g << "NU" << "\n";
}
return 0;
}