Pagini recente » Cod sursa (job #2986701) | Cod sursa (job #2819608) | Cod sursa (job #558385) | Cod sursa (job #2732723) | Cod sursa (job #2971201)
#include <bits/stdc++.h>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
const int N = 2e5 + 5;
int n, m, k, c, nrm, comp[N];
pair<int, int> muchie[2 * N], q[2 * N];
bool u[N];
int getroot(int);
int main()
{
f >> n >> m; k = n;
for (int i = 1; i <= n; i++) comp[i] = i;
for (int i = 1; i <= m; i++) {
f >> muchie[i].first >> muchie[i].second >> q[i].first;
q[i].second = i;
}
sort(q + 1, q + m + 1);
for (int i = 1; i <= m && k != 1; i++){
int cost, poz; tie(cost, poz) = q[i];
int x, y; tie(x, y) = muchie[poz];
x = getroot(x); y = getroot(y);
if (x != y){
comp[x] = y; u[poz] = 1;
k--; c += cost; nrm++;
}
}
g << c << '\n' << nrm << '\n';
for (int i = 1; i <= m; i++)
if (u[i]) g << muchie[i].first << ' ' << muchie[i].second << '\n';
return 0;
}
int getroot(int nod){
if (comp[nod] == nod) return nod;
comp[nod] = getroot(comp[nod]);
return comp[nod];
}