Pagini recente » Cod sursa (job #1890185) | Cod sursa (job #2939880) | Cod sursa (job #2647208) | Cod sursa (job #2683519) | Cod sursa (job #3226027)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
const int M = 400005;
const int N = 200005;
struct muchie {
int x, y, c;
bool operator<(const muchie& other) {
return c < other.c;
}
};
muchie v[M];
int tata[N];
int height[N];
int radacina(int x) {
if (tata[x] == x) return x;
tata[x] = radacina(tata[x]);
return tata[x];
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> v[i].x >> v[i].y >> v[i].c;
}
sort(v + 1, v + m + 1);
for (int i = 1; i <= n; i++) {
tata[i] = i;
height[i] = 1;
}
int ans1 = 0, ans2 = 0;
vector<muchie> ans3;
for (int i = 1; i <= m; i++) {
int rx = radacina(v[i].x), ry = radacina(v[i].y);
if (rx == ry) continue;
if (height[rx] == height[ry]) {
tata[rx] = ry;
height[ry]++;
}
else if (height[rx] <= height[ry]) {
tata[rx] = ry;
}
else {
tata[ry] = rx;
}
ans1 += v[i].c;
ans2++;
ans3.push_back(v[i]);
}
cout << ans1 << '\n' << ans2 << '\n';
for (muchie mu : ans3) {
cout << mu.x << ' ' << mu.y << '\n';
}
return 0;
}