Pagini recente » Cod sursa (job #290670) | Cod sursa (job #63752) | Cod sursa (job #1628737) | Cod sursa (job #300070) | Cod sursa (job #3167813)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
const int nmax = 100003;
int n, m;
int tata[nmax];
int rang[nmax];
void readAndSolve(void);
void unite(int x, int y);
int root(int x);
int main(){
int i;
readAndSolve();
}
void readAndSolve(void){
int a, x, y;
fin>>m>>m;
while(m--){
fin>>a>>x>>y;
if(a == 1)
unite(x, y);
else if(a == 2){
if(root(x) != root(y))
fout<<"NU\n";
else fout<<"DA\n";
}
}
}
void unite(int x, int y){
int rootx, rooty;
rootx = root(x);
rooty = root(y);
if(rootx != rooty){
if(rang[rootx] == rang[rooty]){
tata[rootx] = rooty;
rang[rootx]++;
}
else if(rang[rootx] > rang[rooty])
tata[rootx] = rooty;
else tata[rooty] = rootx;
}
}
int root(int x){
int radacina;
if(!tata[x])
return x;
else{
radacina = root(tata[x]);
tata[x] = radacina;
return radacina;
}
}