Pagini recente » Cod sursa (job #1868558) | Cod sursa (job #271601) | Cod sursa (job #2031541) | Cod sursa (job #70296) | Cod sursa (job #1910558)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int nmax = 200005;
int N,M,t[nmax],ans;
bool viz[2*nmax];
struct edge
{
int x,y,cost;
bool operator < (const edge &A) const
{
return cost < A.cost;
}
};
edge E[2*nmax];
inline void Read()
{
fin >> N >> M;
for(int i = 1; i <= M; i++)
fin >> E[i].x >> E[i].y >> E[i].cost;
}
inline void Union(int x, int y)
{
t[x] = y;
}
inline int Find(int x)
{
int aux,rad = x;
while(rad != t[rad]) rad = t[rad];
while(x != rad)
{
aux = t[x];
t[x] = rad;
x = aux;
}
return rad;
}
inline void Kruskal()
{
sort(E+1,E+M+1);
int i,r1,r2;
for(i = 1; i <= N; i++) t[i] = i;
for(i = 1; i <= M; i++)
{
r1 = Find(E[i].x);
r2 = Find(E[i].y);
if(r1 != r2)
{
ans += E[i].cost;
Union(r1,r2);
viz[i] = true;
}
}
fout << ans << "\n" << N-1 << "\n";
for(i = 1; i <= M; i++) if(viz[i]) fout << E[i].x << " " << E[i].y << "\n";
fout.close();
}
int main()
{
Read();
Kruskal();
return 0;
}