Pagini recente » Cod sursa (job #2656274) | Cod sursa (job #2739057) | Cod sursa (job #2971581) | Cod sursa (job #2520771) | Cod sursa (job #3192408)
#include <fstream>
using namespace std;
ifstream cin("disjoint.in");
ofstream cout("disjoint.out");
int root[100001], sz[100001], n, i, m, c, a, b, x, y;
int findRoot(int x)
{
if (root[x] == x)
return x;
else {
root[x] = findRoot(root[x]);
return root[x];
}
}
int main()
{
cin >> n >> m;
for (i = 1; i <= n; i++)
{
root[i] = i;
sz[i] = 1;
}
for (i = 1; i <= m; i++)
{
cin >> c;
if (c == 1)
{
cin >> x >> y;
if (sz[x] < sz[y])
swap(x, y);
sz[x] += sz[y];
root[y] = root[x];
}
if (c == 2)
{
cin >> x >> y;
a = findRoot(x);
b = findRoot(y);
if (a != b) cout << "NU\n";
else cout <<"DA\n";
}
}
return 0;
}