Pagini recente » Cod sursa (job #459075) | Cod sursa (job #168481) | Cod sursa (job #2099514) | Cod sursa (job #3201775) | Cod sursa (job #2302617)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct Muchie
{
int x, y, cost;
bool check;
bool operator <(const Muchie &A) const
{
return cost < A.cost;
}
};
Muchie a[400001];
int n, m, t[200001];
void Citire()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
fin >> a[i].x >> a[i].y >> a[i].cost;
}
void Union(int x, int y)
{
t[y] = x;
}
int Find(int x)
{
int rad = x;
while(t[rad])
rad = t[rad];
int y;
while(x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void apm()
{
int x, y, i, costMin, nrcc;
sort(a + 1, a + m + 1);
nrcc = n;
costMin = 0;
for(i = 1; i <= m && nrcc > 1; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if(x != y)
{
Union(x, y);
costMin += a[i].cost;
a[i].check = true;
nrcc--;
}
}
fout << costMin << "\n" << n - nrcc << "\n";
for(i = 1; i <= m; i++)
if(a[i].check == true)
fout << a[i].x << " " << a[i].y << "\n";
}
int main()
{
Citire();
apm();
fin.close();
fout.close();
return 0;
}