Pagini recente » Cod sursa (job #974317) | Cod sursa (job #1380940) | Monitorul de evaluare | Rating Jesse Matthews (6harpere132th3) | Cod sursa (job #1012166)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct muchie{ int x, y, c; } v[400005];
struct cmp
{
bool operator()(const muchie &a, const muchie &b)const
{
return (a.c<b.c);
}
};
int N, M, sol[200005], K, GR[200005], cost;
int grupa(int nod)
{
if (GR[nod]==nod) return nod;
GR[nod]=grupa(GR[nod]);
return GR[nod];
}
void uneste(int x, int y)
{
GR[grupa(x)]=grupa(y);
}
int main()
{
f>>N>>M;
for (int i=1; i<=M; ++i)
f>>v[i].x>>v[i].y>>v[i].c;
sort(v+1, v+M+1, cmp());
for (int i=1; i<=N; ++i) GR[i]=i;
for (int i=1; i<=M && K!=N-1; ++i)
{
if ( grupa(v[i].x) != grupa(v[i].y) )
{
cost+=v[i].c; uneste(v[i].x, v[i].y);
sol[++K]=i;
}
}
g<<cost<<'\n'<<K<<'\n';
for (int i=1; i<N; ++i)
g<<v[sol[i]].x<<' '<<v[sol[i]].y<<'\n';
return 0;
}