Pagini recente » Cod sursa (job #2918117) | Cod sursa (job #2935041) | Cod sursa (job #335614) | Cod sursa (job #2908577) | Cod sursa (job #2869917)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchii
{
int x, y, cost;
bool ales;
bool operator < (const muchii w) const
{
return cost < w.cost;
}
} h[400005];
int n, m, costMin, t[200005];
void Union(int x, int y)
{
t[y] = x;
}
int Find(int x)
{
int rad = x, y;
while (t[rad] != 0) rad = t[rad];
while (rad != x)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Kruskal()
{
int i, x, y, nrcc = n;
sort(h + 1, h + m + 1);
for (i = 1; i <= m && nrcc > 1; i++)
{
x = Find(h[i].x);
y = Find(h[i].y);
if (x != y)
{
nrcc--;
Union(x, y);
h[i].ales = true;
costMin += h[i].cost;
}
}
fout << costMin << "\n" << n - 1 << "\n";
for (i = 1; i <= m; i++)
if (h[i].ales) fout << h[i].x << " " << h[i].y << "\n";
}
void Citire()
{
int i;
fin >> n >> m;
for (i = 1; i <= m; i++)
fin >> h[i].x >> h[i].y >> h[i].cost;
fin.close();
}
int main()
{
Citire();
Kruskal();
fout.close();
return 0;
}