Pagini recente » Cod sursa (job #2562627) | Cod sursa (job #3228122) | Cod sursa (job #3164812) | Cod sursa (job #2936127) | Cod sursa (job #1758065)
#include <fstream>
using namespace std;
#define Nmax 100002
ifstream fin( "disjoint.in" );
ofstream fout( "disjoint.out" );
int Tata[Nmax], Grad[Nmax];
int GetRoot( int x ){
int root;
for( root = x; root != Tata[root]; root = Tata[root] );
int aux;
while( x != Tata[x] ){
aux = Tata[x];
Tata[x] = root;
x = aux;
}
return root;
}
void Unite( int x, int y ){
x = GetRoot( x );
y = GetRoot( y );
if( Grad[x] > Grad[y] )
Tata[y] = x;
else
Tata[x] = y;
if( Grad[x] == Grad[y] )
Grad[y]++;
}
int main(){
int N, M;
fin >> N >> M;
for( int i = 1; i <= N; ++i ){
Tata[i] = i;
Grad[i] = 1;
}
int x, a, b;
while( M-- ){
fin >> x >> a >> b;
if( x == 1 )
Unite( a, b );
else
fout << (GetRoot(a) == GetRoot(b) ? "DA" : "NU") << "\n";
}
return 0;
}