Pagini recente » Borderou de evaluare (job #1519368) | Borderou de evaluare (job #2059026) | Borderou de evaluare (job #1466364) | Monitorul de evaluare | Cod sursa (job #3344690)
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 200000;
const int MMAX = 400000;
ifstream f("apm.in");
ofstream g("apm.out");
struct muchie
{
int x, y, c;
};
int n, m;
muchie M[MMAX + 1];
int T[NMAX + 1];
int sol[NMAX + 1], nm;
long long cost;
bool comp(const muchie &a, const muchie &b)
{
return a.c < b.c;
}
void citire()
{
f >> n >> m;
for(int i = 1; i <= m; i++)
f >> M[i].x >> M[i].y >> M[i].c;
}
int Find(int x)
{
if(T[x] == 0) return x;
return T[x] = Find(T[x]);
}
void Kruskal()
{
sort(M + 1, M + m + 1, comp);
cost = 0;
nm = 0;
for(int i = 1; i <= m && nm < n - 1; i++)
{
int rx = Find(M[i].x);
int ry = Find(M[i].y);
if(rx != ry)
{
T[ry] = rx;
cost += M[i].c;
sol[++nm] = i;
}
}
}
void afisare()
{
g << cost << '\n';
g << nm << '\n';
for(int i = 1; i <= nm; i++)
{
int k = sol[i];
g << M[k].x << ' ' << M[k].y << '\n';
}
}
int main()
{
citire();
Kruskal();
afisare();
f.close();
g.close();
return 0;
}