Pagini recente » Cod sursa (job #2215871) | Cod sursa (job #647988) | Cod sursa (job #1685803) | Cod sursa (job #2293957) | Cod sursa (job #3270936)
#include <fstream>
using namespace std ;
ifstream cin ("disjoint.in") ;
ofstream cout ("disjoint.out") ;
int n, q, t[200001] ;
int get_root (int x)
{
if (t[x] == x)
return x ;
return t[x] = get_root(t[x]) ;
}
void unite(int x, int y)
{
if (t[x] > t[y])
t[x] = t[y] ;
else
t[y] = t[x] ;
}
int main()
{
cin >> n >> q ;
for (int i = 1 ; i <= 200000 ; i ++)
t[i] = i ;
for (int i = 1 ; i <= q ; i ++)
{
int op, x, y ;
cin >> op >> x >> y ;
x = get_root (x) ;
y = get_root (y) ;
if (op == 1)
{
if (x != y)
unite (x, y) ;
}
else
cout << (x == y ? "DA \n" : "NU \n") ;
}
return 0 ;
}