Pagini recente » Cod sursa (job #2897929) | Cod sursa (job #2081141) | Cod sursa (job #756296) | Cod sursa (job #3030612) | Cod sursa (job #3240413)
#include <fstream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n, m, t[200001], r[200001];
struct muchie {
int x, y, c;
bool operator < (const muchie& other) const {
return this->c < other.c;
}
}M[400001];
void Read()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
fin >> M[i].x >> M[i].y >> M[i].c;
}
int Find(int x) {
if (t[x] == 0) return x;
int k = Find(t[x]);
t[x] = k;
return k;
}
void Union(int x, int y)
{
if (r[x] < r[y])
t[x] = y;
else
{
t[y] = x;
if (r[x] == r[y])
r[x]++;
}
}
void Kruskal() {
sort(M + 1, M + m + 1);
int totalcost = 0;
vector <pair<int, int> > sol;
for (int i = 1; i <= m; i++)
{
int x = Find(M[i].x);
int y = Find(M[i].y);
if (x != y) {
Union(x, y);
totalcost += M[i].c;
sol.push_back({ M[i].x, M[i].y });
}
}
fout << totalcost << "\n" << sol.size() << "\n";
for (auto it : sol)
fout << it.first << " " << it.second << "\n";
}
int main()
{
Read();
Kruskal();
return 0;
}