Pagini recente » Cod sursa (job #630603) | Cod sursa (job #720193) | Cod sursa (job #2515784) | Cod sursa (job #1814506) | Cod sursa (job #984424)
Cod sursa(job #984424)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct Element{
int x;
int y;
int c;
};
Element Array[400002];
int N,M,Solution[400002];
int R[200002],TT[200002],Result,Number;
bool V[400002];
bool compare(Element a,Element b)
{
return a.c<b.c;
}
void Read()
{
f>>N>>M;
int i;
for(i=1;i<=M;i++)
f>>Array[i].x>>Array[i].y>>Array[i].c;
sort(Array+1,Array+M+1,compare);
}
int Father(int x)
{
while(TT[x]!=x)
x=TT[x];
while(TT[x]!=x)
{
int y=TT[x];
TT[x]=x;
x=y;
}
return x;
}
void Unite(int x,int y)
{
if(R[x]<R[y])
TT[x]=y;
else
TT[y]=x;
if(R[x]==R[y])
R[x]++;
}
void Solve()
{
int i;
for(i=1;i<=N;i++)
TT[i]=i;
for(i=1;i<=M;i++)
{
if(Father(Array[i].x)!=Father(Array[i].y))
Unite(Father(Array[i].x),Father(Array[i].y)),Result+=Array[i].c,Number++,Solution[Number]=i;
}
g<<Result<<"\n"<<Number<<"\n";
for(i=1;i<=Number;i++)
g<<Array[Solution[i]].x<<" "<<Array[Solution[i]].y<<"\n";
}
int main()
{
Read();
Solve();
return 0;
}