Mai intai trebuie sa te autentifici.
Cod sursa(job #1714280)
| Utilizator | Data | 7 iunie 2016 22:02:48 | |
|---|---|---|---|
| Problema | Paduri de multimi disjuncte | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.86 kb |
#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";
}
}
}
