Pagini recente » Cod sursa (job #1431528) | Cod sursa (job #1741031)
#include <fstream>
using namespace std;
ifstream in("disjoint.in");
ofstream out("disjoint.out");
int N,M,op,x,y;
int rank[100003],parent[100003];
void make_set(int x) {
parent[x]=x;
rank[x]=0;
}
int find_set(int x) {
if (x!=parent[x])
parent[x]=find_set(parent[x]);
return parent[x];
}
void union_set(int x,int y) {
int px=find_set(x);
int py=find_set(y);
if (rank[px]>rank[py]) {
parent[py]=px;
}
else {
parent[px]=py;
}
if (rank[px]==rank[py]) {
parent[py]=px;
rank[px]++;
}
}
int main() {
in>>N>>M;
for (int i=1;i<=N;i++) {
make_set(i);
}
for (int i=1;i<=M;i++) {
in>>op>>x>>y;
//make_set(x); make_set(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;
}