Pagini recente » Cod sursa (job #2160350) | Cod sursa (job #59890) | Cod sursa (job #50456) | Cod sursa (job #114532) | Cod sursa (job #2162170)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct arbore {
int x, y, cost;
bool operator <(const arbore &A) const {
return A.cost > cost;
}
};
const int nMax = 400002;
int n, m, ans;
int t[nMax / 2];
bool viz[nMax];
arbore a[nMax];
void Read() {
int i;
fin >> n >> m;
for (i = 1; i <= m; i++) {
fin >> a[i].x >> a[i].y >> a[i].cost;
}
fin.close();
}
void Union(int x, int y) {
t[y] = x;
}
int Find(int x) {
int rad, y;
rad = x;
while (t[rad]) {
rad = t[rad];
}
while (t[x]) {
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Solve() {
int i, x, y;
sort(a + 1, a + m + 1);
for (i = 1; i <= m; i++) {
x = Find(a[i].x);
y = Find(a[i].y);
if (x != y) {
Union(x, y);
viz[i] = 1;
ans += a[i].cost;
}
}
fout << ans << "\n" << n - 1 << "\n";
for (i = 1; i <= m; i++) {
if (viz[i]) {
fout << a[i].x << " " << a[i].y << "\n";
}
}
fout.close();
}
int main() {
Read();
Solve();
return 0;
}