Pagini recente » Statistici Alexandra Toma (ale336-t) | Cod sursa (job #2751009) | Cod sursa (job #2063645) | Monitorul de evaluare | Cod sursa (job #2681366)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int t[100002], r[100002];
void u(int x, int y) {
if (r[x]+1>r[y]+1) {
t[y]=x;
} else if (r[x]+1<r[y]+1) {
t[x]=y;
} else {
t[y]=x;
r[x]++;
}
}
int f(int x) {
while (t[x]!=0) x=t[x];
return x;
}
int main() {
int N, M, cod, x, y, i, tx, ty;
in >> N >> M;
for (i=1; i<=N; i++)
for (i=0; i<M; i++) {
in >> cod >> x >> y;
if (cod==1) {
tx=f(x);
ty=f(y);
u(tx, ty);
} else {
tx=f(x);
ty=f(y);
if (tx==ty) out << "DA\n";
else out << "NU\n";
}
}
return 0;
}