Pagini recente » Cod sursa (job #2336778) | Cod sursa (job #1216779) | Cod sursa (job #1971393) | Cod sursa (job #784090) | Cod sursa (job #3040600)
/// kruskal
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in ("apm.in");
ofstream out ("apm.out");
const int max_size = 2e5 + 1;
struct str{
int x, y, c;
bool operator < (const str & aux) const
{
return c < aux.c;
}
};
int t[max_size];
vector <str> mc;
vector <pair <int, int>> ans;
int rad (int x)
{
if (x == t[x])
{
return x;
}
return t[x] = rad(t[x]);
}
int main ()
{
int n, m;
in >> n >> m;
for (int i = 1; i <= n; i++)
{
t[i] = i;
}
int rez = 0;
while (m--)
{
int x, y, c;
in >> x >> y >> c;
mc.push_back({x, y, c});
}
sort(mc.begin(), mc.end());
for (auto f : mc)
{
int rx = rad(f.x), ry = rad(f.y);
if (rx != ry)
{
rez += f.c;
t[rx] = ry;
ans.push_back({f.x, f.y});
}
}
out << rez << '\n' << ans.size() << '\n';
for (auto f : ans)
{
out << f.first << " " << f.second << '\n';
}
in.close();
out.close();
return 0;
}