Pagini recente » Cod sursa (job #1378828) | Cod sursa (job #257269) | Cod sursa (job #2838593) | Cod sursa (job #2527657) | Cod sursa (job #3153234)
//#include <bits/stdc++.h>
#include <fstream>
#include <iostream>
//#include <vector>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#define int long long
//#define ll long long
using namespace std;
const int NMAX = 1e5;
int father[NMAX + 1];
int root( int node ) {
int f, x, aux;
for ( f = node; father[f] > 0; f = father[f] );
for ( x = node; father[x] > 0; ) {
aux = father[x];
father[x] = f;
x = aux;
}
return f;
}
void unite( int a, int b ) {
a = root( a );
b = root( b );
if ( father[a] > father[b] ) swap( a, b );
father[a] += father[b] - 1;
father[b] = a;
}
bool isUnite( int a, int b ) {
return ( root( a ) == root( b ) );
}
int main() {
ifstream fin( "disjoint.in" );
ofstream fout( "disjoint.out" );
int n, q;
fin >> n >> q;
for ( int i = 1, a, b, tip; i <= q; i ++ ) {
fin >> tip >> a >> b;
switch( tip ) {
case 1:
unite( a, b );
break;
default:
if ( isUnite( a, b ) ) {
fout << "DA\n";
} else {
fout << "NU\n";
}
}
}
return 0;
}