Pagini recente » Cod sursa (job #989546) | Cod sursa (job #2020692) | Cod sursa (job #2003083) | Cod sursa (job #1768543) | Cod sursa (job #2147968)
#include <cstdio>
#include <algorithm>
using namespace std;
const int nmax=200000, mmax=400000;
int t[nmax+3], h[nmax+3];
struct muchie{int x; int y; int cost;};
muchie v[mmax+3],sol[nmax+3];
bool cmp(muchie a, muchie b)
{
return a.cost<b.cost;
}
int radacina(int x)
{
while(t[x]!=x)
{
x=t[x];
}
return x;
}
int unire(int x, int y)
{
if(h[x]==h[y])
{
t[y]=x;
h[x]++;
}
else if(h[x]>h[y])
t[y]=x;
else t[x]=y;
}
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
int n,m,i;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
t[i]=i;
h[i]=1;
}
for(i=1;i<=m;i++)
scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].cost);
sort(v+1,v+m+1,cmp);
int nr=0,a,b,total=0;
for(i=1;i<=m&&nr<n;i++)
{
if(radacina(v[i].x)!=radacina(v[i].y))
{
a=radacina(v[i].x); b=radacina(v[i].y);
unire(a,b);
nr++;
total+=v[i].cost;
sol[nr]=v[i];
}
}
printf("%d\n%d\n",total,nr);
for(i=1;i<=nr;i++)
printf("%d %d\n",sol[i].x,sol[i].y);
return 0;
}