Pagini recente » Cod sursa (job #1132573) | Cod sursa (job #1835819) | Cod sursa (job #1076696) | Cod sursa (job #397022) | Cod sursa (job #2547379)
#include <fstream>
using namespace std;
const int N = 100001;
const int M = 100001;
int lst[N], vf[2*M], urm[2*M], n, nr[N], t[N];
int nrorig;
int radacina (int x) {
if ( t[x] == 0 )
return x;
t[x] = radacina(t[x]);
return t[x];
}
void reuniune (int x, int y ) {
int rx = radacina(x);
int ry = radacina(y);
if ( rx == ry )
return;
if ( nr[rx] < nr[ry] ) {
t[rx] = ry;
nr[ry] += nr[rx];
} else {
t[ry] = rx;
nr[rx] += nr[ry];
}
}
bool interogare (int x, int y) {
return ( radacina(x) == radacina(y) );
}
int main () {
ifstream fin ("disjoint.in");
ofstream fout ("disjoint.out");
int n, op, cod, a, b;
fin>>n>>op;
for ( int opcount = 0; opcount < op; opcount++ ) {
fin>>cod>>a>>b;
if ( cod == 1 )
reuniune(a,b);
else {
if ( interogare(a,b) )
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
return 0;
}