Pagini recente » Cod sursa (job #809340) | Cod sursa (job #1617789) | Cod sursa (job #147552) | Statistici Czupper Mihaela Carina (mihaelacarina) | Cod sursa (job #3285214)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n, m, t[200005], k;
pair<int, int> v[400005];
struct muchie
{
int x, y, c;
}a[400005];
int Comp(muchie a, muchie b)
{
if (a.c < b.c)
return 1;
return 0;
}
int Find(int x)
{
int y;
if (t[x] == 0)
return x;
y = Find(t[x]);
t[x] = y;
return y;
}
void Union(int x, int y)
{
t[y] = x;
}
int main()
{
int i, x, y, cost = 0;
fin >> n >> m;
for (i = 1 ; i <= m ; i++)
fin >> a[i].x >> a[i].y >> a[i].c;
sort(a + 1, a + m + 1, Comp);
for (i = 1 ; i <= m ; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if (x != y)
{
cost += a[i].c;
v[++k] = {a[i].x, a[i].y};
Union(x, y);
}
}
fout << cost << "\n";
fout << k << "\n";
for (i = 1 ; i <= k ; i++)
fout << v[i].first << " " << v[i].second << "\n";
return 0;
}