Pagini recente » Cod sursa (job #2936256) | Cod sursa (job #3185805) | Cod sursa (job #846682) | Cod sursa (job #2496886) | Cod sursa (job #2750282)
#include <fstream>
int t[100001];
int rang[100001];
int GetRoot(int k) {
if (t[k] == k)
return k;
int root = GetRoot(t[k]);
t[k] = root;
return root;
}
bool SameRoot(int x, int y) {
return GetRoot(x) == GetRoot(y);
}
void Unite(int x, int y) {
int rx = GetRoot(x);
int ry = GetRoot(y);
if (rx == ry)
return;
if (rang[rx] > rang[ry])
t[ry] = rx;
else {
t[rx] = ry;
if (rang[rx] == rang[ry])
++rang[ry];
}
}
int main() {
std::ifstream fin("disjoint.in");
std::ofstream fout("disjoint.out");
int N, M;
fin >> N >> M;
for (int i = 1; i <= N; ++i)
t[i] = i;
int cod, x, y;
while (M--) {
fin >> cod >> x >> y;
if (cod == 1) {
Unite(x, y);
} else if (cod == 2) {
if (SameRoot(x, y))
fout << "DA\n";
else
fout << "NU\n";
}
}
fin.close();
fout.close();
return 0;
}