Pagini recente » Cod sursa (job #2430395) | Cod sursa (job #2590679) | Cod sursa (job #2462326) | Cod sursa (job #2576343) | Cod sursa (job #3215124)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct vrajeala
{
int x, y, cost;
bool operator < (const vrajeala A) const
{
return cost < A.cost;
}
};
int n, m, cost_total;
int t[400005];
vrajeala L[400005];
vector<pair<int, int>> sol;
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 (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
int i, x, y;
fin >> n >> m;
for (i = 1; i <= m; i++)
fin >> L[i].x >> L[i].y >> L[i].cost;
sort(L + 1, L + m + 1);
for (i = 1; i <= m; i++)
{
x = Find(L[i].x);
y = Find(L[i].y);
if (x != y)
{
cost_total += L[i].cost;
Union(x, y);
sol.push_back({L[i].x, L[i].y});
}
}
fout << cost_total << "\n";
fout << sol.size() << "\n";
for (pair<int, int> e : sol)
fout << e.first << " " << e.second << "\n";
return 0;
}