Pagini recente » Cod sursa (job #1767158) | Cod sursa (job #2948515) | Cod sursa (job #1117541) | Cod sursa (job #2152656) | Cod sursa (job #1135074)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int MAXN = 100005;
int N, M;
int father[MAXN];//tatal nodului
int rang[MAXN];
void Init() {
for (int i = 1; i <= N; ++i) {
rang[i] = 1;
father[i] = i;
}
}
int Find(int a) {
int x = a;
while (father[x] != x)
x = father[x];
while (father[a] != a) {
int aux = father[a];
father[a] = x;
a = aux;
}
return x;
}
void Unite(int a, int b) {
if (rang[a] > rang[b]) swap(a, b);
father[a] = b;
++rang[b];
}
string Query(int a, int b) {
if (Find(a) == Find(b))
return "DA";
else return "NU";
}
int main() {
fin >> N >> M;
Init();
int op, a, b;
for (int i = 0; i < M; ++i) {
fin >> op >> a >> b;
if (op == 1)
Unite(Find(a), Find(b));
else
fout << Query(a, b) << '\n';
}
fin.close();
fout.close();
return 0;
}