Pagini recente » Cod sursa (job #953417) | Istoria paginii utilizator/otiliuta | Statistici Buda Elena-Dabiela (NekoDana) | Monitorul de evaluare | Cod sursa (job #2681364)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in ("disjoint.in");
ofstream out ("disjoint.out");
int t[100000], r[100000];
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;
}