Pagini recente » Cod sursa (job #1861000) | Cod sursa (job #514251) | Cod sursa (job #1976317) | Cod sursa (job #400392) | Cod sursa (job #2864346)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
struct muchie
{
int x, y, c;
} v[400005];
int n, m, x, y;
int ans, nr;
int cc[400005];
bool used[400005];
bool cmp (muchie a, muchie b)
{
return a.c < b.c;
}
int conex (int x)
{
if(cc[x]==x)
return x;
cc[x] = conex(cc[x]);
return cc[x];
}
void unificare (int x, int y)
{
cc[conex(x)]=conex(y);
}
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
fin>>v[i].x>>v[i].y>>v[i].c;
cc[i]=i;
}
sort(v+1,v+m+1,cmp);
for(int i=1; i<=m; i++)
{
if(conex(v[i].x) != conex(v[i].y))
{
ans+=v[i].c;
used[i]=1;
nr++;
}
unificare(cc[v[i].x], cc[v[i].y]);
}
fout<<ans<<'\n'<<nr<<'\n';
for(int j=1; j<=m; j++)
if(used[j]==1)
fout<<v[j].x<<" "<<v[j].y<<'\n';
return 0;
}