Pagini recente » Cod sursa (job #3217119) | Cod sursa (job #1416552) | Cod sursa (job #1411111) | Cod sursa (job #128338) | Cod sursa (job #1741061)
#include <fstream>
using namespace std;
int parent[100001];
int ranks[100001];
void make_set(int x) {
parent[x]=x;
ranks[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(px>py) {
parent[py]=px;
}
else if(py>px) {
parent[px]=py;
}
else {
ranks[py]=px;
ranks[px]++;
}
}
int main() {
ifstream in("disjoint.in");
ofstream out("disjoint.out");
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) {
union_set(x,y);
}
else {
if (find_set(x)==find_set(y)) {
out<<"DA\n";
}
else {
out<<"NU\n";
}
}
}
return 0;
}