Pagini recente » Cod sursa (job #3228080) | Cod sursa (job #3293622) | Rating Motoc Alexandru (alexdmotoc) | Cod sursa (job #2846061) | Cod sursa (job #3293471)
#include <fstream>
#include <algorithm>
#define nmax (int)(2e5+1)
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
int n,m,t[nmax],mch,cost;
bool in[2*nmax];
struct muchie{
int x,y,c;
}v[2*nmax];
int get_root(int x){
if(t[x]>0)
return get_root(t[x]);
return x;
}
int join(int x,int y){
x=get_root(x),y=get_root(y);
if(x==y)
return 0;
if(t[x]>t[y])
swap(x,y);
t[x]+=t[y];
t[y]=x;
return 1;
}
bool cmp(const muchie& a,const muchie& b){
return a.c<b.c;
}
void kruskal(){
for(int i=1;i<=n;i++)
t[i]=-1;
sort(v+1,v+m+1,cmp);
for(int i=1;i<=m&&mch<n;i++)
if(join(v[i].x,v[i].y)){
mch++;
in[i]=1;
cost+=v[i].c;
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>v[i].x>>v[i].y>>v[i].c;
kruskal();
cout<<cost<<'\n'<<n-1<<'\n';
for(int i=1;i<=m;i++)
if(in[i])
cout<<v[i].x<<" "<<v[i].y<<'\n';
return 0;
}