Pagini recente » Cod sursa (job #2179451) | Cod sursa (job #1648687) | Cod sursa (job #1601284) | Cod sursa (job #2341388) | Cod sursa (job #2547374)
#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) );
}
void adauga ( int x, int y ) {
vf[++nrorig] = y;
urm[nrorig] = lst[x];
lst[x] = nrorig;
t[y] = x;
nr[radacina(x)]++;
}
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 )
adauga(a,b);
else {
if ( interogare(a,b) )
fout<<"DA"<<'\n';
else
fout<<"NU"<<'\n';
}
}
return 0;
}