Pagini recente » Cod sursa (job #2806853) | Cod sursa (job #2053201) | Cod sursa (job #2807005) | Cod sursa (job #2813262) | Cod sursa (job #3187506)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct Muchie
{
int x, y, c;
bool operator<(const Muchie e)const
{
return c < e.c;
}
};
int n, m;
int t[200005];
Muchie a[400005];
queue<pair<int, int>> sol;
int FindRoot(int x)
{
int y, rad = x;
while (t[rad] != 0)
rad = t[rad];
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Union(int x, int y)
{
t[y] = x;
}
int main()
{
int i, x, y, c, cost;
fin >> n >> m;
for (i = 1; i <= m; i++)
fin >> a[i].x >> a[i].y >> a[i].c;
cost = 0;
sort(a + 1, a + m + 1);
for (i = 1; i <= m; i++)
{
x = a[i].x; y = a[i].y; c = a[i].c;
x = FindRoot(x); y = FindRoot(y);
if (x != y)
{
cost += c;
Union(x, y);
sol.push({a[i].x, a[i].y});
}
}
fout << cost << "\n" << sol.size() << "\n";
while (!sol.empty())
{
fout << sol.front().first << " " << sol.front().second << "\n";
sol.pop();
}
}