Pagini recente » Cod sursa (job #516553) | Cod sursa (job #2729647) | Cod sursa (job #1935119) | Cod sursa (job #1456555) | Cod sursa (job #2468915)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int a[400001];
int ct(int nod)
{
if(a[nod]>0)
return ct(a[nod]);
else
return nod;
}
int s,x,y,n,m;
struct vec
{
int x,y,d;
}v[400001];
bool viz[400001];
int cmp(vec x, vec y)
{
return x.d<y.d;
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>v[i].x>>v[i].y>>v[i].d;
if(i<=n)
a[i]=-1;
}
sort(v+1,v+m+1,cmp);
int k=0,t=1;
while(k<n-1)
{
int x=v[t].x,tx=ct(x);
int y=v[t].y,ty=ct(y);
if(tx!=ty)
{
if(a[tx]<a[ty])
{
a[tx]+=a[ty];
a[ty]=tx;
}
else
{
a[ty]+=a[tx];
a[tx]=ty;
}
s+=v[t].d;
viz[t]=1;
k++;
}
t++;
}
fout<<s<<'\n'<<k<<'\n';
for(int i=1;i<=m;i++)
if(viz[i]==1)
fout<<v[i].x<<' '<<v[i].y<<'\n';
return 0;
}