Pagini recente » Cod sursa (job #2719563) | Cod sursa (job #2687133) | Cod sursa (job #1359356) | Cod sursa (job #2126249) | Cod sursa (job #1894169)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie
{
int x,y,c;
};
vector <int> Sol;
int N,M,TT[200005],R[200005],Cost;
muchie V[400005];
void Read()
{
fin>>N>>M;
for(int i=1;i<=M;i++)
{
fin>>V[i].x>>V[i].y>>V[i].c;
}
}
int compare(muchie a,muchie b)
{
return (a.c < b.c);
}
int Father(int nod)
{
while(nod!=TT[nod])
{
nod=TT[nod];
}
return nod;
}
void Unite(int x,int y)
{
if(R[y]>R[x])
TT[x]=y;
else
TT[y]=x;
if(R[x]==R[y])
R[x]++;
}
void Solve()
{
sort(V+1,V+M+1,compare);
for(int i=1;i<=N;i++)
{
TT[i]=i;
}
for(int i=1;i<=M;i++)
{
int X = Father(V[i].x);
int Y = Father(V[i].y);
if(X != Y)
{
Unite(X,Y);
Cost+=V[i].c;
Sol.push_back(i);
}
}
}
void Print()
{
fout<<Cost<<"\n"<<N-1<<"\n";
for(int i=0;i<(int)Sol.size();i++)
fout<<V[Sol[i]].x<<" "<<V[Sol[i]].y<<"\n";
}
int main()
{
Read();
Solve();
Print();
return 0;
}