Pagini recente » Cod sursa (job #994416) | Cod sursa (job #2475730) | Cod sursa (job #2565724) | Cod sursa (job #215839) | Cod sursa (job #2695583)
#include <fstream>
using namespace std;
const int NMAX = 100001;
int n,m;
int reprez[NMAX];
int componenta[NMAX];
int find_reprez(int node) {
if (node == reprez[node]) {
return node;
}
int nreprez = find_reprez(reprez[node]);
reprez[node] = nreprez;
return nreprez;
}
int main() {
ifstream fin("disjoint.in");
ofstream fout("disjoint.out");
fin >> n >> m;
for (int i = 1; i <= n; i++) {
reprez[i] = i;
componenta[i] = 1;
}
for (int i = 1; i <= m; i++) {
int opt, x, y;
fin >> opt >> x >> y;
int reprez_x = find_reprez(x);
int reprez_y = find_reprez(y);
if (opt == 1) {
if (reprez_x == reprez_y)
continue;
if (componenta[reprez_x] > componenta[reprez_y]) {
reprez[reprez_y] = reprez_x;
componenta[reprez_x] += componenta[y];
}
else {
reprez[reprez_x] = reprez_y;
componenta[reprez_y] += componenta[reprez_y];
}
}
else {
if (reprez_x == reprez_y) {
fout << "DA\n";
}
else {
fout << "NU\n";
}
}
}
fin.close();
fout.close();
return 0;
}