Pagini recente » Cod sursa (job #1637151) | Cod sursa (job #2882641) | Cod sursa (job #706949) | Cod sursa (job #2873840) | Cod sursa (job #2784145)
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int lim = 1e5 + 5;
int t[lim];
int sz[lim];
int n, m;
int find(int x)
{
if (!t[x])
return x;
return t[x] = find(t[x]);
}
void unite(int x, int y)
{
int tx = find(x), ty = find(y);
if (tx != ty)
{
if (sz[tx] > sz[ty])
t[ty] = tx;
else
{
t[tx] = ty;
if (sz[tx] == sz[ty])
sz[ty]++;
}
}
}
int main()
{
freopen("disjoin.in", "r", stdin);
freopen("disjoin.out", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int op, x, y;
cin >> op >> x >> y;
if (op == 1)
unite(x, y);
else
{
if (find(x) == find(y))
cout << "DA\n";
else
cout << "NU\n";
}
}
}