Pagini recente » Cod sursa (job #877059) | Cod sursa (job #921928) | Cod sursa (job #1345929) | Cod sursa (job #2393110) | Cod sursa (job #3314396)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n, m, t[200001], k;
pair <int, int> e[400001];
struct muchii
{
int x, y, c;
}a[400001];
bool Comp(muchii x, muchii y)
{
return x.c < y.c;
}
void Union(int x, int y)
{
t[y] = x;
}
int Find(int x)
{
int y;
if (t[x] == 0)
return x;
y = Find(t[x]);
t[x] = y;
return y;
}
int main()
{
int i, x, y, c, cost = 0, nr;
fin >> n >> m;
nr = n;
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 && nr > 1 ; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if (x != y)
{
Union(x, y);
cost += a[i].c;
--nr;
++k;
e[k].first = a[i].x;
e[k].second = a[i].y;
}
}
fout << cost << "\n";
fout << k << "\n";
for (i = 1 ; i <= k ; i++)
fout << e[i].first << " " << e[i].second << "\n";
return 0;
}