Pagini recente » Cod sursa (job #2159741) | Cod sursa (job #323856) | Cod sursa (job #967331) | Cod sursa (job #1473852) | Cod sursa (job #742471)
Cod sursa(job #742471)
#include <stdio.h>
#include <math.h>
#include<fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
long n, m, i, a, b, c, tata[100010], aux, nivel[100010], max;
long parc(long nod, long o) {
if (aux != -1)
if (o == 1) tata[nod] = aux;
if (nivel[nod] > max) max = nivel[nod];
if (tata[nod]) parc(tata[nod], o);
else {
if (aux == -1) {
aux = nod;
nivel[nod] = max;
}
return aux;
}
}
int main() {
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
f >> n >> m;
for (i = 1; i <= m; ++i) {
f >> c >> a >> b;
if (c == 1) {
aux = -1;max = 0;
long A = parc(a, 0);
aux = -1;max = 0;
long B = parc(b, 0);
if (nivel[A] < nivel[B]) {
tata[A] = B;
nivel[A] = nivel[B] + 1;
} else {
tata[B] = A;
nivel[B] = nivel[A] + 1;
}
}
if (c == 2) {
aux = -1;max = 0;
long u = parc(a, 0);
aux = -1;max = 0;
long y = parc(b, 0);
if (u == y) {
g << "DA\n";
} else {
g << "NU\n";
}
aux = -1;max = 0;
if (tata[a]) parc(a, 1);
aux = -1;max = 0;
if (tata[b]) parc(b, 1);
}
}
return 0;
}