Pagini recente » Cod sursa (job #1143497) | Cod sursa (job #1448535) | Cod sursa (job #1826128) | Cod sursa (job #578971) | Cod sursa (job #3216546)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin ("apm.in");
ofstream fout("apm.out");
const int n_Max = 2e5 + 1;
const int m_Max = 4e5 + 1;
int n, m, cost;
struct muchii
{
int x, y, c;
} a[m_Max];
vector <pair <int, int>> b(1);
vector <int> tata(n_Max, -1), dim(n_Max, 1);
void read()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
fin >> a[i].x >> a[i].y >> a[i].c;
}
int find_set(int x)
{
if (tata[x] == -1)
return x;
return tata[x] = find_set(tata[x]);
}
void sets_union(int x, int y)
{
if (dim[x] < dim[y])
swap(x, y);
dim[x] += dim[y];
tata[y] = x;
}
bool comp(muchii a, muchii b)
{
return a.c < b.c;
}
void kruskal()
{
sort(a + 1, a + 1 + m, comp);
for (int i = 1; i <= m; ++i)
{
int x = find_set(a[i].x);
int y = find_set(a[i].y);
if (x != y)
{
sets_union(x, y);
cost += a[i].c;
b.push_back({a[i].x, a[i].y});
}
}
}
void print()
{
fout << cost << "\n" << n - 1 << "\n";
for (int i = 1; i < n; ++i)
fout << b[i].first << " " << b[i].second << "\n";
}
int main()
{
read();
kruskal();
print();
fin.close();
fout.close();
return 0;
}