Pagini recente » Cod sursa (job #2736808) | Cod sursa (job #3120573) | Cod sursa (job #2752906) | Cod sursa (job #2182199) | Cod sursa (job #2684665)
#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("disjoint.in" , "r" , stdin) , freopen("disjoint.out" , "w" , stdout)
using namespace std;
const int N = 1e5 + 10;
int n , m , x , y , type;
int roots[N] , heights[N];
int F(int x)
{
int R = x;
for( ; R != roots[R] ; R = roots[R]);
for( ; roots[x] != x ; )
{
int next = roots[x];
roots[x] = R;
x = next;
}
}
void unite(int x , int y)
{
int rx = F(x);
int ry = F(y);
if(rx == ry)
return;
if(heights[rx] < heights[ry])
roots[rx] = ry;
else if(heights[rx] > heights[ry])
roots[ry] = rx;
else
{
roots[rx] = ry;
++heights[ry];
}
}
signed main()
{
#ifndef ONLINE_JUDGE
FastIO , FILES;
#endif
cin >> n >> m;
for(int i = 1 ; i <= n ; i++)
{
roots[i] = i;
heights[i] = 1;
}
for(int i = 1 ; i <= m ; i++)
{
cin >> type >> x >> y;
if(type == 1)
unite(x , y);
else if(F(x) == F(y))
cout << "DA\n";
else cout << "NU\n";
}
return 0;
}