Pagini recente » Cod sursa (job #340999) | Cod sursa (job #630662) | Cod sursa (job #2751308) | Cod sursa (job #931505) | Cod sursa (job #2930709)
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
#ifdef LOCAL
ifstream fin("input.txt");
#define fout cout
#else
ifstream fin("apm.in");
ofstream fout("apm.out");
#endif
const int NMAX = 2e5+5;
const int MMAX = 2e5+5;
struct Edge {
int x, y, c;
};
int n, m;
Edge e[MMAX];
int r[NMAX];
int cost_min;
vector<pair<int, int>> ans;
void read() {
fin >> n >> m;
for (int i = 1; i <= m; i++)
fin >> e[i].x >> e[i].y >> e[i].c;
}
int rep(int x) {
if (r[x] == x)
return x;
r[x] = rep(r[x]);
return r[x];
}
bool cmp(const Edge &a, const Edge &b) {
return a.c < b.c;
}
void merge(Edge &e) {
int rx = rep(e.x), ry = rep(e.y);
if (rx == ry)
return;
cost_min += e.c;
r[ry] = rx;
ans.push_back({e.y, e.x});
}
void solve() {
for (int i = 1; i <= n; i++)
r[i] = i;
sort(e + 1, e + m + 1, cmp);
for (int i = 1; i <= m; i++)
merge(e[i]);
fout << cost_min << '\n' << ans.size() << '\n';
for (auto a : ans)
fout << a.first << ' ' << a.second << '\n';
}
int32_t main() {
read();
solve();
return 0;
}