Pagini recente » Cod sursa (job #67514) | Cod sursa (job #1619789) | Cod sursa (job #3285930) | Cod sursa (job #100835) | Cod sursa (job #1260506)
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,i,x,y,c,z,x1,yy,t,j,b[200004],mch[200004],h[200004];
long long s;
struct nod
{
int x,y;
short c;
}a[400003];
int tata(int x)
{
if(x==b[x])return x;
else return tata(b[x]);
}
bool cmp(nod a, nod b)
{
if(a.c>b.c)return 0;
return 1;
}
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
scanf("%d %d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&c);
if(x<y){a[i].x=x;a[i].y=y;}
else {a[i].x=y;a[i].y=x;}
a[i].c=c;
}
for(i=1;i<=n;i++){b[i]=i;h[i]=1;}
sort(a+1,a+m+1,cmp);
for(i=1;i<=m;i++)
{
x=a[i].x;
y=a[i].y;
c=a[i].c;
x1=tata(x);
yy=tata(y);
if(x1!=yy)
{
if(h[x1]<h[yy]) b[x1]=yy;
else b[yy]=x1;
if(h[x1]==h[yy]) h[yy]++;
s=s+c;
z++;
mch[z]=i;
if(z==n-1)break;
}
}
printf("%d\n",s);
printf("%d\n",z);
for(i=1;i<=z;i++)
printf("%d %d\n",a[mch[i]].x,a[mch[i]].y);
return 0;
}