Pagini recente » Cod sursa (job #174662) | Cod sursa (job #550628) | Cod sursa (job #1980388) | Cod sursa (job #850204) | Cod sursa (job #2695581)
#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) {
if (componenta[x] > componenta[y]) {
reprez[y] = x;
componenta[x] += componenta[y];
}
else {
reprez[x] = y;
componenta[y] += componenta[x];
}
}
}
else {
if (reprez_x == reprez_y) {
fout << "DA\n";
}
else {
fout << "NU\n";
}
}
}
return 0;
}