Pagini recente » Cod sursa (job #1397240) | Cod sursa (job #194267) | Cod sursa (job #639753) | Cod sursa (job #3179672) | Cod sursa (job #3286236)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie
{
int x,y,c, ales;
};
bool cmp(muchie a, muchie b)
{
return a.c < b.c;
}
int n,m, costTotal, t[400005];
muchie a[400005];
void Union(int x, int y)
{
t[y] = x;
}
int Find(int x)
{
int y, rad;
rad = x;
while(t[rad] != 0)
rad = t[rad];
while(x != rad)
{
y = t[x];
t[x] = rad;
x = y;
}
return rad;
}
void Kruskal()
{
sort(a + 1, a + m + 1, cmp);
int nrc, i, x, y, cnt = 0;
nrc = n;
for(i = 1; i <= m && nrc > 1; i++)
{
x = Find(a[i].x);
y = Find(a[i].y);
if(x != y)
{
Union(x,y);
nrc--;
a[i].ales = 1;
cnt++;
costTotal += a[i].c;
}
}
fout << costTotal << "\n" << cnt << "\n";
for(i = 1; i <= m; i++)
if(a[i].ales == 1)
fout << a[i].y << " " << a[i].x << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
int i,j,c;
fin >> n >> m;
for(i = 1; i <= m; i++)
fin >> a[i].x >> a[i].y >> a[i].c;
Kruskal();
return 0;
}