Pagini recente » Cod sursa (job #3131433) | Cod sursa (job #2411848) | Cod sursa (job #1491133) | Cod sursa (job #93419) | Cod sursa (job #2166257)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
const int N = 200001, M = 400001;
struct muchie {
int x, y, cost;
};
muchie e[M];
int n, m, v[N], ne;
int t[N], nr[N], ind[N];
bool cond(const muchie &e1, const muchie &e2) {
return (e1.cost < e2.cost);
}
int radacina(int x) {
if (t[x] == 0) return x;
t[x] = radacina(t[x]);
return t[x];
}
bool query(int x, int y) {
return (radacina(x) == radacina(y));
}
void reunion(int x, int y) {
int rx = radacina(x), ry = radacina(y);
if (nr[rx] >= nr[ry]) {
t[ry] = rx;
nr[rx] += nr[ry];
}
else {
t[rx] = ry;
nr[ry] += nr[rx];
}
}
int main()
{
in >> n >> m;
for (int i = 0; i < m; i++) {
ne++;
in >> e[ne].x >> e[ne].y >> e[ne].cost;
}
sort(e + 1, e + m + 1, cond);
int i = 1, j = 1, s = 0;
while (j <= n - 1) {
if (!query(e[i].x, e[i].y)) {
ind[j++] = i;
s += e[i].cost;
reunion(e[i].x, e[i].y);
}
i++;
}
out << s << '\n' << n - 1 << '\n';
for (i = 1; i < j; i++) out << e[ ind[i] ].x << ' ' << e[ ind[i] ].y << '\n';
out.close();
}