Pagini recente » Cod sursa (job #2267885) | Cod sursa (job #1539553) | Cod sursa (job #2721741) | Cod sursa (job #86835) | Cod sursa (job #2866942)
#include <iostream>
#include <fstream>
#define NMAX 100005
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int t[NMAX], r[NMAX];
int N, M;
void read() {
fin >> N >> M;
for (int i = 1; i <= N; ++i) {
t[i] = i;
}
}
int findRoot(int x) {
if (t[x] == x) {
return x;
}
t[x] = findRoot(t[x]);
return t[x];
}
void mergeSets(int x, int y) {
x = findRoot(x);
y = findRoot(y);
if (r[x] < r[y]) {
swap(x, y);
}
if (r[x] == r[y]) {
r[x]++;
}
t[y] = x;
}
int main()
{
read();
int a, b, c;
for (int i = 0; i < M; ++i) {
fin >> a >> b >> c;
if (a == 2) {
if (findRoot(b) == findRoot(c)) {
fout << "DA\n";
}
else {
fout << "NU\n";
}
}
else {
mergeSets(b, c);
}
}
return 0;
}