Pagini recente » Cod sursa (job #210146) | Cod sursa (job #11223) | Cod sursa (job #925263) | Monitorul de evaluare | Cod sursa (job #2984136)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct vrajeala
{
int x, y, c;
bool ales;
bool operator< (const vrajeala A) const
{
return c < A.c;
}
};
int n, m, t[200005], costm;
vrajeala a[400005];
void Union(int x, int y)
{
t[x] = y;
}
int Find(int x)
{
int rad = x, y;
while (t[rad] != 0)
rad = t[rad];
while (x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
int x, y, nleg;
fin >> n >> m;
for (int i = 1; i <= m; i++)
fin >> a[i].x >> a[i].y >> a[i].c;
sort(a + 1, a + m + 1);
nleg = 0;
for (int i = 1; i <= m && nleg < n; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if (x != y)
{
Union(x, y);
nleg++;
a[i].ales = 1;
costm += a[i].c;
}
}
fout << costm << "\n";
fout << nleg << "\n";
for (int i = 1; i <= m; i++)
if (a[i].ales) fout << a[i].x << " " << a[i].y << "\n";
return 0;
}