Pagini recente » Cod sursa (job #1292857) | Cod sursa (job #2220833) | Cod sursa (job #2144864) | Cod sursa (job #855845) | Cod sursa (job #2912990)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
int x, y, cod, n, m, tt[100001], sz[100001];
int Find(int nod)
{
if (tt[nod] == nod)
return nod;
return tt[nod] = Find(tt[nod]);
}
void Union(int a, int b)
{
if (a == b)
return;
if (sz[a] >= sz[b]) {
sz[a] += sz[b];
tt[b] = a;
}
else {
sz[b] += sz[a];
tt[a] = b;
}
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= n; i++) {
tt[i] = i;
sz[i] = 1;
}
for (int i = 1; i <= m; i++) {
fin >> cod >> x >> y;
if (cod == 2) {
if(Find(tt[x]) == Find(tt[y]))
fout << "DA\n";
else
fout << "NU\n";
}
else if (cod == 1) {
Union(x, y);
}
}
//for (int i = 1; i <= n; i++)
// cout << tt[i] << " ";
return 0;
}