Pagini recente » Cod sursa (job #1489303) | Cod sursa (job #2946443) | Cod sursa (job #2746838) | Cod sursa (job #2826543) | Cod sursa (job #1714280)
#include <fstream>
using namespace std;
ifstream f("disjoint.in");
ofstream g("disjoint.out");
#define NMAX 100010
int n, m, type, x, y, r[NMAX], t[NMAX];
int root(int nod) {
int root, temp;
for (root = nod;root != t[root];root = t[root]);
for (temp = nod;temp != t[temp];) {
temp = t[nod];
t[nod] = root;
nod = temp;
}
return root;
}
void join(int x, int y) {
if (r[x] > r[y]) t[y] = x;
else t[x] = y;
if (r[x] == r[y]) r[y]++;
}
int main() {
f >> n >> m;
for (int i = 1;i <= n;i++){
r[i] = 1;
t[i] = i;
}
for (int i = 1;i <= m;i++) {
f >> type >> x >> y;
if (type == 1) {
join(root(x), root(y));
}
else {
if (root(x) == root(y)) g<<"DA\n";
else g<<"NU\n";
}
}
}