Pagini recente » Cod sursa (job #1748744) | Cod sursa (job #4324) | Cod sursa (job #2847618) | Cod sursa (job #674089) | Cod sursa (job #2064617)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
struct nod {
nod *t;
int v;
} noduri[100001];
int main()
{
int N, M;
f >> N >> M;
for (int i = 1; i <= N; i++) {
noduri[i].v = i;
noduri[i].t = NULL;
}
nod *c, *d;
for (int i = 1; i <= M; i++) {
int q, x, y;
f >> q >> x >> y;
if (q == 1) {
if (noduri[x].t != NULL) {
c = noduri[x].t;
while (c->t != NULL)
c = c->t;
}
if (noduri[y].t != NULL) {
d= noduri[y].t;
while (d->t != NULL)
d = d->t;
}
if (noduri[x].t == NULL && noduri[y].t == NULL)
noduri[y].t = noduri+x;
else if (c->v != d->v)
d->t = c;
} else {
int t1, t2;
if (noduri[x].t != NULL) {
c = noduri[x].t;
while (c->t != NULL)
c = c->t;
t1 = c->v;
}
else t1 = noduri[x].v;
if (noduri[y].t != NULL) {
c = noduri[y].t;
while (c->t != NULL)
c = c->t;
t2 = c->v;
}
else t2 = noduri[y].v;
if (t1 == t2) g << "DA\n";
else g << "NU\n";
}
}
return 0;
}