Pagini recente » Cod sursa (job #106526) | Cod sursa (job #2521630) | Cod sursa (job #2228510) | Cod sursa (job #1281622) | Cod sursa (job #3167803)
#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];
void readAndSolve(void);
void unite(int x, int y);
int root(int x);
int main(){
int i;
for(i = 1; i<= n; i++)
tata[i] = 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)
tata[rootx] = rooty;
}
int root(int x){
int radacina;
if(!tata[x])
return x;
else{
radacina = root(tata[x]);
tata[x] = radacina;
return radacina;
}
}