Pagini recente » Cod sursa (job #952837) | Cod sursa (job #1116239) | Cod sursa (job #2846522) | Cod sursa (job #2618943) | Cod sursa (job #2432884)
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
int N, Q;
int Father[Nmax];
int findSet(int node)
{
while(Father[node] != node)
node = Father[node];
return Father[node];
}
void unionSet(int first, int second)
{
int findFirst = findSet(first);
int findSecond = findSet(second);
if(findFirst != findSecond)
Father[findSecond] = findFirst;
else
Father[findFirst] = findSecond;
}
int main()
{
freopen("disjoint.in", "r", stdin);
freopen("disjoint.out", "w", stdout);
scanf("%d %d", &N, &Q);
for(int i = 1; i <= N; ++i)
Father[i] = i;
while(Q--)
{
int operation, first, second;
scanf("%d %d %d", &operation, &first, &second);
if(operation == 1)
unionSet(first, second);
else if(findSet(first) == findSet(second))
printf("DA\n");
else
printf("NU\n");
}
return 0;
}