Pagini recente » Cod sursa (job #1550491) | Cod sursa (job #360401) | Cod sursa (job #1467013) | Cod sursa (job #554555) | Cod sursa (job #3265992)
#include <fstream>
#include <algorithm>
#define mmax (int)(4e5+1)
#define nmax (int)(2e5+1)
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
int n,m,t[nmax],apm;
bool ok[mmax];
struct muchie{
int x,y,c;
}a[mmax];
bool cmp(const muchie& a,const muchie& b){
return a.c<b.c;
}
int get_root(int x){
if(t[x]>0)
return get_root(t[x]);
return x;
}
void join(int x,int y,int c,int k){
x=get_root(x);
y=get_root(y);
if(x==y)
return ;
if(t[x]>t[y])
swap(x,y);
t[x]+=t[y];
t[y]=x;
apm+=c;
ok[k]=1;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>a[i].x>>a[i].y>>a[i].c;
sort(a+1,a+m+1,cmp);
for(int i=1;i<=n;i++)
t[i]=-1;
for(int i=1;i<=m;i++)
join(a[i].x,a[i].y,a[i].c,i);
cout<<apm<<'\n'<<n-1<<'\n';
for(int i=1;i<=m;i++)
if(ok[i])
cout<<a[i].x<<" "<<a[i].y<<'\n';
return 0;
}