Pagini recente » Cod sursa (job #1611915) | Cod sursa (job #3173958) | Cod sursa (job #1692683) | Cod sursa (job #1436604) | Cod sursa (job #3154126)
#include <fstream>
using namespace std;
ifstream fin;
ofstream fout;
int n;
int arr[100005];
struct DSU {
int parent[n + 5];
void init() {
for(int i = 1; i <= n; i++) {
parent[i] = i;
}
}
int find(int target) {
if(parent[target] == target) {
return target;
}
return find(parent[target]);
}
void unite(int x, int y) {
parent[find(y)] = find(x);
}
};
int main()
{
fin.open("disjoint.in");
fin >> n;
int m;
fin >> m;
DSU dsu;
dsu.init();
int command;
int x, y;
fout.open("disjoint.out");
for(int i = 1; i <= m; i++) {
fin >> command >> x >> y;
switch(command) {
case 1:{
dsu.unite(x, y);
}
case 2:{
if(dsu.find(x) == dsu.find(y)) {
fout << "DA" << endl;
} else {
fout << "NU" << endl;
}
}
}
}
fin.close();
fout.close();
return 0;
}