Pagini recente » Cod sursa (job #2664674) | Cod sursa (job #1931761) | Cod sursa (job #1358756) | Cod sursa (job #1417287) | Cod sursa (job #2936311)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
int p[200001], n, m;
pair<pair<int, int>, int> muchii[400001];
long long cost_minim;
vector<pair<int, int> > muchiiapm;
bool cmp(pair<pair<int, int>, int> muchie1, pair<pair<int, int>, int> muchie2){
return muchie1.second < muchie2.second;
}
int parinte(int nod1){
while(p[nod1] != 0){
nod1 = p[nod1];
}
return nod1;
}
int main()
{
int x, y, c;
in >> n >> m;
for(int i = 1; i <= m; i++){
in >> x >> y >> c;
muchii[i] = {{x, y}, c};
}
sort(muchii + 1, muchii + m + 1, cmp);
for(int i = 1; i <= m; i++){
if(parinte(muchii[i].first.first) != parinte(muchii[i].first.second)){
muchiiapm.push_back({muchii[i].first.first, muchii[i].first.second});
cost_minim += muchii[i].second;
p[max(parinte(muchii[i].first.first), parinte(muchii[i].first.second))] = min(parinte(muchii[i].first.first), parinte(muchii[i].first.second));
}
}
out << cost_minim << '\n';
out << muchiiapm.size() << '\n';;
for(int i = 0; i < muchiiapm.size(); i++){
out << muchiiapm[i].first << " " << muchiiapm[i].second<< '\n';
}
return 0;
}