Pagini recente » Cod sursa (job #2633352) | Cod sursa (job #253185) | Cod sursa (job #3167346) | Cod sursa (job #1004569) | Cod sursa (job #1987348)
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<vector<int> > arrays;
vector<int> where;
void megaPouring(int from, int to)
{
while(!arrays[from].empty()) {
arrays[to].push_back( arrays[from].back() );
arrays[from].pop_back();
where[arrays[to].back()] = to;
}
arrays[from].shrink_to_fit();
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
ios::sync_with_stdio(false);
scanf("%d%d", &N, &M);
arrays.resize(N + 1);
where.resize(N + 1);
for(int i = 1; i <= N; ++i)
{
arrays[i].push_back(i);
where[i] = i;
}
int tip, a, b;
for(int i = 1; i <= M; ++i)
{
scanf("%d%d%d",&tip, &a, &b);
if(tip == 2) {
if(where[a] == where[b])
printf("DA\n");
else
printf("NU\n");
continue;
}
int x, y;
x = where[a];
y = where[b];
if(arrays[x].size() > arrays[y].size())
swap(x, y);
megaPouring(x, y);
}
return 0;
}