Pagini recente » Cod sursa (job #503584) | Comisia | Cod sursa (job #374466) | Cod sursa (job #374417) | Cod sursa (job #1962057)
#include <cstdio>
#include <algorithm>
using namespace std;
struct muchie
{
int x,y,c;
} a[400005];
int n,m,i,r1,r2,muchii,cost,solx[400005],t[200005];
bool cmp(muchie a, muchie b)
{
return a.c<=b.c;
}
int tata(int x)
{
while (t[x]>0)
x=t[x];
return x;
}
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",&a[i].x,&a[i].y,&a[i].c);
sort(a+1,a+m+1,cmp);
for (i=1; i<=n; i++)
t[i]=-1;
for (i=1; i<=m&&muchii<n-1; i++)
{
r1=tata(a[i].x);
r2=tata(a[i].y);
if (r1!=r2)
{
muchii++;
solx[muchii]=i;
cost=cost+a[i].c;
if(t[r1]<t[r2]) {
t[r1]+=t[r2];
t[r2]=r1;
}
else
{
t[r2]+=t[r1];
t[r1]=r2;
}
}
}
printf("%d\n%d\n",cost,muchii);
for (i=1; i<=muchii; i++)
printf("%d %d\n",a[solx[i]].x,a[solx[i]].y);
return 0;
}