Pagini recente » Cod sursa (job #1272978) | Cod sursa (job #2586442) | Cod sursa (job #1877240) | Cod sursa (job #1588705) | Cod sursa (job #3156244)
#include <algorithm>
#include <fstream>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int MAXIM = 400005;
struct muchie {
int x, y, cost;
};
pair<int, int> muchii_finale[MAXIM];
muchie muchii[MAXIM];
int n, m, cost_total = 0, nr_muchii_finale = 0, parinte[MAXIM], rang[MAXIM];
bool comparare(muchie a, muchie b) {
return a.cost < b.cost;
}
inline void citire() {
fin >> n >> m;
for (int i = 0; i < m; i++) {
fin >> muchii[i].x >> muchii[i].y >> muchii[i].cost;
}
}
inline void init() {
sort(muchii, muchii + m, comparare);
for (int i = 0; i < m; i++) {
parinte[i] = i;
}
}
int root(int nod) {
while (parinte[nod] != nod) {
nod = parinte[nod];
}
return nod;
}
inline void rezolva() {
for (int i = 0; i < m; i++) {
int root1 = root(muchii[i].x);
int root2 = root(muchii[i].y);
if (root1 == root2) {
continue;
}
parinte[root1] = root2;
muchii_finale[nr_muchii_finale].first = muchii[i].x;
muchii_finale[nr_muchii_finale].second = muchii[i].y;
nr_muchii_finale++;
cost_total += muchii[i].cost;
}
}
inline void afiseaza() {
// cost total
fout << cost_total << '\n';
// muchii in arbore total
fout << n - 1 << '\n';
// muchii in arbore
for (int i = 0; i < nr_muchii_finale; i++) {
fout << muchii_finale[i].first << ' ' << muchii_finale[i].second << '\n';
}
}
int main() {
citire();
init();
rezolva();
afiseaza();
return 0;
}