Pagini recente » Cod sursa (job #3195929) | Cod sursa (job #2634415) | Cod sursa (job #1636067) | Cod sursa (job #2822470) | Cod sursa (job #2797244)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std ;
ifstream fin("disjoint.in") ;
ofstream fout("disjoint.out") ;
int n , m , t[100005] , h[100005] , tx , ty ;
int findSet(int x) {
while(t[x]!=x)
x = t[x] ;
return x ;
}
void unionSet(int x , int y) {
if(h[x] > h[y]) {
t[y] = x ;
} else if(h[y] > h[x]) {
t[x] = y ;
} else {
t[y] = x ;
h[x]++ ;
}
}
int main() {
int x , y , operatie;
fin >> n >> m ;
for(int i = 1 ;i<=n ;i++) {
t[i] = i ;
h[i] = 1 ;
}
for(int i = 1 ;i<=m ;i++) {
fin >> operatie >> x >> y ;
if(operatie == 1) {
tx = findSet(x) ;
ty = findSet(y) ;
if(tx != ty) {
unionSet(tx , ty) ;
}
} else if(operatie == 2){
tx = findSet(t[x]) ;
ty = findSet(t[y]) ;
if( tx == ty )
fout << "DA" << '\n' ;
else fout << "NU" << '\n' ;
}
}
return 0;
}