Pagini recente » Monitorul de evaluare | Cod sursa (job #3851) | Cod sursa (job #3262159) | Cod sursa (job #3219512) | Cod sursa (job #3285867)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n, m, t[200001], nr, k;
pair<int, int> v[400001];
struct muchie
{
int x, y, c;
bool operator < (muchie m) const
{
return c < m.c;
}
}a[400001];
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, cost = 0;
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);
for (i = 1; i <= m && nr > 1 ; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if (x != y)
{
--nr;
Union(x, y);
cost += a[i].c;
v[++k] = {a[i].x, a[i].y};
}
}
fout << cost << "\n";
fout << k << "\n";
for (i = 1 ; i <= k ; i++)
fout << v[i].first << " " << v[i].second << "\n";
return 0;
}