Pagini recente » Cod sursa (job #317489) | Cod sursa (job #3146016) | Cod sursa (job #2853605) | Cod sursa (job #495388) | Cod sursa (job #2635810)
#include <iostream>
#include <fstream>
#define DIM 10005
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
int t[DIM],rang[DIM],n,m;
int radacina(int nod){
if(t[nod] == 0)
return nod;
else{
int x = radacina(t[nod]);
t[nod] = x;
return x;
}
}
void op1(int x, int y){
int rx = radacina(x), ry=radacina(y);
if(rang[rx]>rang[ry])
t[ry]=rx;
else{
t[rx]=ry;
if(rang[rx]==rang[ry])
rang[ry]++;
}
}
void op2(int x, int y){
int rx=radacina(x), ry=radacina(y);
if(rx==ry)
g<<"DA\n";
else
g<<"NU\n";
}
int main()
{
f>>n>>m;
int op,x,y;
for(int k=1; k<=m; k++){
f>>op>>x>>y;
if(op==1)
op1(x,y);
else
op2(x,y);
}
}