Pagini recente » Cod sursa (job #267676) | Cod sursa (job #2895102) | Cod sursa (job #567780) | Cod sursa (job #356813) | Cod sursa (job #2134179)
#include <iostream>
#include <vector>
#include <fstream>
#include <stack>
#define NMAX (100000 + 3)
#define MMAX (1000000 + 3)
using namespace std;
void Unite(int a, int b, int father[]) {
while (father[a] > 0) {
a = father[a];
}
while (father[b] > 0) {
b = father[b];
}
father[b] = a;
}
bool Check(int a, int b, int father[]) {
while (father[a] > 0) {
a = father[a];
}
while (father[b] > 0) {
b = father[b];
}
return father[a] == father[b];
}
int main() {
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int n, q;
f >> n >> q;
int father[NMAX];
for (int i = 1; i <= n; ++i) {
father[i] = -i;
}
for (; q; --q) {
int type, a, b;
f >> type >> a >> b;
if (type == 1) {
Unite(a, b, father);
} else {
if (Check(a, b, father)) {
g << "DA\n";
} else {
g << "NU\n";
}
}
}
return 0;
}