Pagini recente » Cod sursa (job #1642613) | Cod sursa (job #2460894) | Cod sursa (job #3140040) | Cod sursa (job #1176658) | Cod sursa (job #2130237)
#include <iostream>
#include <fstream>
#include <algorithm>
#define MAX 200001
using namespace std;
struct muchie{
int x,y,c;
bool fol;
};
int n,m,ans,ansf;
int p[MAX];
muchie a[MAX*2];
bool cmp(muchie m1,muchie m2){ return m1.c<m2.c; }
int findr(int ind){
if(p[ind]==ind)return ind;
p[ind]=findr(p[ind]);
return p[ind];
}
void reuneste(int i1,int i2){ p[findr(i1)]=findr(i2); }
int main()
{
ifstream f ("apm.in");
ofstream g ("apm.out");
f>>n>>m;
for(int i=1;i<=m;i++) f>>a[i].x>>a[i].y>>a[i].c,a[i].fol=true;
sort(a+1,a+m+1,cmp);
for(int i=1;i<=n;i++)p[i]=i;
for(int i=1;i<=m;i++){
if(findr(a[i].x)==findr(a[i].y))a[i].fol=false;
else reuneste(a[i].x,a[i].y),ans+=a[i].c,ansf++;
}
g<<ans<<'\n'<<ansf<<'\n';
for(int i=1;i<=m;i++)if(a[i].fol)g<<a[i].x<<" "<<a[i].y<<'\n';
f.close ();
g.close ();
return 0;
}