Pagini recente » Cod sursa (job #1431549) | Cod sursa (job #1265352) | Cod sursa (job #3223389) | Cod sursa (job #1432160) | Cod sursa (job #1741033)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int r[10010];
int p[10010];
void make_set(int x) {
p[x]=x;
r[x]=0;
}
int find_set(int x) {
if (x!=p[x])
p[x]=find_set(p[x]);
return p[x];
}
void union_set(int x,int y) {
int px=find_set(x);
int py=find_set(y);
if (r[px]>r[py]) {
p[py]=px;
}
else {
p[px]=py;
}
if (r[px]==r[py]) {
p[py]=px;
r[px]++;
}
}
int main() {
int N,M,op,x,y;
in>>N>>M;
for (int i=1;i<N;i++) {
make_set(i);
}
for (int i=1;i<=M;i++) {
in>>op>>x>>y;
if (op==1) {
union_set(x,y);
}
else {
if (find_set(x)==find_set(y)) {
out<<"DA\n";
}
else {
out<<"NU\n";
}
}
}
return 0;
}