Pagini recente » Cod sursa (job #892385) | Cod sursa (job #1521177) | Cod sursa (job #3217257) | Cod sursa (job #1927322) | Cod sursa (job #2897504)
#include <bits/stdc++.h>
using namespace std;
int n, m;
int t[200005];
vector<pair<int, pair<int, int>>> v;
void U(int x, int y)
{
t[y] = x;
}
int find(int x)
{
if (t[x] == x)
return x;
else
return(t[x] = find(t[x]));
}
int main()
{
ifstream fin("apm.in");
ofstream fout("apm.out");
fin >> n >> m;
for (int i = 1; i <= n; ++i)
t[i] = i;
for (int j = 1; j <= m; j++)
{
int x, y, w;
fin >> x >> y >> w;
v.push_back({w, {x, y}});
}
sort(v.begin(), v.end());
int cost = 0;
vector<pair<int, int>> ans;
for (int i = 0; i < (int)v.size(); ++i)
{
int x, y;
x = find(v[i].second.first);
y = find(v[i].second.second);
if (x != y)
{
U(x, y);
cost += v[i].first;
ans.push_back({v[i].second.first, v[i].second.second});
}
}
fout << cost << "\n";
fout << (int)ans.size() << '\n';
for (auto i : ans)
fout << i.second << " " << i.first << '\n';
return 0;
}