Pagini recente » Istoria paginii runda/oni2011_ziua2 | Cod sursa (job #2847282) | Cod sursa (job #2704249) | Cod sursa (job #2315359) | Cod sursa (job #2701330)
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5 + 100;
int parent[mxN];
int st[mxN];
char buff[mxN * 3];
char * buffp = buff;
#define FL(nm) do { freopen(nm ".in", "r", stdin); freopen(nm ".out", "w", stdout); } while (false)
int main() {
FL("disjoint");
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n + 1; i++) {
parent[i] = i;
}
while (m--) {
int o, i, j;
scanf("%d%d%d", &o, &i, &j);
if (o == 1) {
parent[j] = parent[i];
} else if (o == 2) {
int pi = i;
int pj = j;
int sti = 0;
while (parent[pi] != pi) {
st[sti++] = pi;
pi = parent[pi];
}
while (sti--) {
parent[st[sti]] = pi;
}
sti = 0;
while (parent[pj] != pj) {
st[sti++] = pj;
pj = parent[pj];
}
while (sti--) {
parent[st[sti]] = pj;
}
if (pi == pj) {
strcpy(buffp, "DA\n");
} else {
strcpy(buffp, "NU\n");
}
buffp += 3;
}
}
puts(buff);
return 0;
}