Pagini recente » Cod sursa (job #1494312) | Cod sursa (job #2289459) | Cod sursa (job #3179817) | Cod sursa (job #523717) | Cod sursa (job #3030334)
#include <bits/stdc++.h>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");
int n, m, t[100005], costMin;
struct Muchie
{
int x, y, cost, ales;
}a[4000005];
bool Cmp(Muchie A, Muchie B)
{
return A.cost < B.cost;
}
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(rad != x)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
int main()
{
int i, j, x, y, cost,nrcc, nr = 0;
in >> n >> m;
for(i = 1; i <= m; i++)
in >> a[i].x >> a[i].y >> a[i].cost;
sort(a + 1, a +m +1, Cmp);
nrcc = n;
for(i = 1; i <= m and nrcc > 1; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if(x != y)
{
costMin += a[i].cost;
a[i].ales = 1;
nr++;
Union(x, y);
nrcc--;
}
}
out << costMin << "\n" << nr << "\n";
for(i = 1; i <= m; i++)
if(a[i].ales == 1)
out << a[i].x << " " << a[i].y << "\n";
return 0;
}