Pagini recente » Cod sursa (job #1712464) | Cod sursa (job #3313765) | Cod sursa (job #3230762) | Monitorul de evaluare | Cod sursa (job #3322967)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie{
int x, y;
int c;
} v[400001];
int sef[200001];
int findd(int x){
int sefmare;
if(sef[x]!=x){
sefmare=findd(sef[x]);
sef[x]=sefmare;
return sef[x];
}
return x;
}
void unire(int x, int y){
int sefx, sefy;
sefx=findd(x);
sefy=findd(y);
sef[sefy]=sef[sefx];
}
bool comp(muchie x, muchie y){
return x.c<=y.c;
}
int main()
{
int n, m, i, s=0, nrm=0;
fin>>n>>m;
for(i=1;i<=m;i++)
fin>>v[i].x>>v[i].y>>v[i].c;
for(i=1;i<=n;i++)
sef[i]=i;
sort(v+1, v+m+1, comp);
for(i=1;i<=m;i++){
if(findd(v[i].x)!=findd(v[i].y)){
s=s+v[i].c;
nrm++;
unire(v[i].x, v[i].y);
}
}
fout<<s<<endl<<nrm<<endl;
for(i=1;i<=m;i++)
if(sef[v[i].x]!=sef[v[i].y])
fout<<v[i].x<<" "<<v[i].y<<endl;
return 0;
}