Pagini recente » Cod sursa (job #1742975) | Cod sursa (job #1468703) | Cod sursa (job #1936650) | Cod sursa (job #121715) | Cod sursa (job #3005567)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n,m,total,nrm,k;
struct muchie{
int x,y,cost;
}v[400001];
int tt[200001],rg[200001];
pair<int,int> p[200001];
bool compare(muchie a,muchie b){
return a.cost<b.cost;
}
int Find(int nod){
while(tt[nod]!=nod)
nod=tt[nod];
return nod;
}
void unire(int x,int y){
if(rg[x]<rg[y])
tt[x]=y;
else if(rg[x]>rg[y])
tt[y]=x;
else{
tt[x]=y;
rg[y]++;
}
}
void res(){
for(int i=1;i<=m;++i){
int tatalx=Find(v[i].x);
int tataly=Find(v[i].y);
if(tatalx!=tataly){
unire(tatalx,tataly);
nrm++;
p[++k].first=v[i].y;
p[k].second=v[i].x;
total+=v[i].cost;
}
}
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;++i)
fin>>v[i].x>>v[i].y>>v[i].cost;
for(int i=1;i<=n;++i)
tt[i]=i,rg[i]=1;
sort(v+1,v+m+1,compare);
res();
fout<<total<<'\n';
fout<<nrm<<'\n';
for(int i=1;i<=k;++i)
fout<<p[i].first<<" "<<p[i].second<<'\n';
return 0;
}