Pagini recente » Cod sursa (job #2632328) | Cod sursa (job #2826804) | Cod sursa (job #2087450) | Cod sursa (job #2011965) | Cod sursa (job #2260901)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int NMax = 200000;
const int MMax = 400000;
struct Edge
{
int a,b,c;
};
Edge E[MMax + 5];
int N,M,K;
int TT[NMax + 5],R[NMax + 5],Index[NMax + 5],Sol;
bool Compare(Edge A, Edge B)
{
return A.c < B.c;
}
void Read()
{
fin >> N >> M;
for(int i = 1; i <= M; ++i)
fin >> E[i].a >> E[i].b >> E[i].c;
sort(E + 1, E + M + 1, Compare);
for(int i = 1; i <= N; ++i)
TT[i] = i;
}
void Unite(int x, int y)
{
if(R[x] < R[y])
TT[x] = y;
if(R[x] > R[y])
TT[y] = x;
if(R[x] == R[y])
{
TT[x] = y;
R[y]++;
}
}
int Root(int x)
{
while(TT[x] != x)
x = TT[x];
return x;
}
void Solve()
{
for(int i = 1; i<= M; ++i)
{
int A = E[i].a, B = E[i].b, C = E[i].c;
if(Root(A) != Root(B))
{
Unite(Root(A),Root(B));
Sol += C;
Index[++K] = i;
}
}
}
void Print()
{
fout << Sol << "\n" << N - 1 << "\n";
for(int i = 1; i <= K; ++i)
fout << E[Index[i]].a <<" " << E[Index[i]].b<< "\n";
}
int main()
{
Read();
Solve();
Print();
return 0;
}