Pagini recente » Cod sursa (job #3340486) | Cod sursa (job #1786715) | Cod sursa (job #2125555) | Monitorul de evaluare | Cod sursa (job #3348866)
#include <fstream>
#include <algorithm>
#define nmax 200001
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
int n,m,x,y,tip,t[nmax],cost,nr;
bool in[nmax];
struct mch{
int x,y,c;
}v[nmax];
bool cmp(const mch& a,const mch& b){
return a.c<b.c;
}
int get_root(int nod){
if(t[nod]>0)
return t[nod]=get_root(t[nod]);
return nod;
}
bool 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;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
t[i]=-1;
for(int i=1;i<=m;i++)
cin>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,cmp);
for(int i=1;i<=m;i++)
if(join(v[i].x,v[i].y))
cost+=v[i].c,nr++,in[i]=1;
cout<<cost<<'\n'<<nr<<'\n';
for(int i=1;i<=m;i++)
if(in[i])
cout<<v[i].x<<" "<<v[i].y<<'\n';
return 0;
}