Pagini recente » Cod sursa (job #2462735) | Cod sursa (job #1779303) | Borderou de evaluare (job #1567371) | Cod sursa (job #1800372) | Cod sursa (job #2848522)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
const int MAXN = 100000;
int father[MAXN];
int myRank[MAXN];
int find( int x ){
if( father[x] == x )
return x;
return father[x] = find(father[x]);
}
void unite( int x, int y ){
int rootx, rooty;
rootx = find(x);
rooty = find(y);
if( rootx != rooty ){
if( myRank[rootx] < myRank[rooty] ){
father[rootx] = rooty;
}
else if( myRank[rootx] > myRank[rooty] ){
father[rooty] = rootx;
}
else{
father[rootx] = rooty;
myRank[rootx]++;
}
}
}
int main(){
int n, m, i, x, y, cod;
in>>n>>m;
for( i = 0; i < n; i++ ){
father[i] = i;
myRank[i] = 1;
}
for( i = 0; i < m; i++ ){
in>>cod>>x>>y;
if( cod == 1 ){
unite(x - 1, y - 1);
}
else{
if(find(x - 1) == find(y - 1))
out<<"DA"<<'\n';
else
out<<"NU"<<'\n';
}
}
return 0;
}