Pagini recente » Cod sursa (job #504890) | Cod sursa (job #1577853) | Cod sursa (job #2448261) | Cod sursa (job #1126664) | Cod sursa (job #3254914)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n, m;
int c, x, y;
int ctot;
int t[100005], inalt[100005];
vector<pair<int, pair<int, int>>> v;
vector<pair<int, int>> res;
int rad(int nod){
if(nod == t[nod]) return nod;
int rnod = rad(t[nod]);
t[nod] = rnod;
return rnod;
}
void reuniune(){
int rx = rad(x);
int ry = rad(y);
if(inalt[rx] > inalt[ry]){
t[ry]=rx;
}
else if(inalt[rx] < inalt[ry]){
t[rx]=ry;
}
else{
t[ry]=rx;
inalt[rx]++;
}
}
bool interogare(){
return (rad(x)==rad(y));
}
int main()
{
fin>>n>>m;
for(int i=1; i<=n; i++) t[i]=i;
for(int i=1; i<=m; i++){
fin>>x>>y>>c;
v.push_back({c, {x, y}});
}
sort(v.begin(), v.end());
for(auto k:v){
x=k.second.first;
y=k.second.second;
if(!interogare()){
reuniune();
ctot+=k.first;
res.push_back({x, y});
}
}
fout<<ctot<<'\n';
fout<<res.size()<<'\n';
for(auto k : res){
fout<<k.first<<' '<<k.second<<'\n';
}
return 0;
}