Pagini recente » Cod sursa (job #1946614) | Cod sursa (job #362448) | Cod sursa (job #2281569) | Cod sursa (job #2591874) | Cod sursa (job #1828466)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
const int MAXM = 4e5+5, MAXN = 2e5+5;
int x[MAXN], y[MAXN], c[MAXN];
int tt[MAXN], ind[MAXN], n, m, i, j;
int sol[MAXM], ns, st;
bool cmp(int a, int b) { return c[a] < c[b]; }
void join(int x, int y) { tt[x] = y; }
int update(int x) {
int r = x, y;
while (tt[r] != r)
r = tt[r];
while (tt[x] != x) {
y = tt[x];
tt[x] = r;
x = y;
}
return r;
}
int main() {
f >> n >> m;
for (i = 1; i <= n; i++)
tt[i] = i;
for (i = 1; i <= m; i++) {
f >> x[i] >> y[i] >> c[i];
ind[i] = i;
}
sort(ind+1, ind+m+1, cmp);
for (i = 1; i <= m; i++)
if (update(x[ind[i]]) != update(y[ind[i]])) {
join(update(x[ind[i]]), update(y[ind[i]]));
st += c[ind[i]];
sol[++ns] = ind[i];
}
g << st << '\n' << ns << '\n';
for (i = 1; i <= ns; i++)
g << x[sol[i]] << ' ' << y[sol[i]] << '\n';
return 0;
}