Pagini recente » Cod sursa (job #1705724) | Cod sursa (job #1138716) | Cod sursa (job #2665088) | Cod sursa (job #2867282) | Cod sursa (job #3163161)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int nmax=400005;
pair<int,int> P[nmax];
int n,m,total,t[nmax],rg[nmax],k;
struct muchie{
int x,y,c;
}v[nmax];
bool comp(muchie a,muchie b){
return a.c<b.c;
}
void Fin(){
fin>>n>>m;
for(int i=1;i<=m;i++)
fin>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,comp);
for(int i=1;i<=n;i++)
t[i]=i;
}
int cauta(int x){
while(t[x]!=x)
x=t[x];
return x;
}
int unire(int x,int y){
if(rg[x]<rg[y])
t[x]=y;
if(rg[x]>rg[y])
t[y]=x;
if(rg[x]==rg[y]){
t[x]=y;
rg[y]++;
}
}
void kruskal(){
for(int i=1;i<=m;i++){
int xx=cauta(v[i].x),yy=cauta(v[i].y);
if(xx!=yy){
unire(xx,yy);
P[++k].first=xx;
P[k].second=yy;
total+=v[i].c;
}
}
}
void afisare(){
fout<<total<< '\n';
fout<<n-1<<'\n';
for(int i=1;i<=k;i++)
fout<<P[i].first<< " "<<P[i].second<< '\n';
}
int main()
{
Fin();
kruskal();
afisare();
return 0;
}