Pagini recente » Cod sursa (job #2496626) | Cod sursa (job #1026686) | Cod sursa (job #3137696) | Cod sursa (job #384577) | Cod sursa (job #3153235)
//#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
#include <fstream>
//#include <vector>
//#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;
}