Pagini recente » Cod sursa (job #1681326) | Cod sursa (job #950936) | Cod sursa (job #2388576) | Cod sursa (job #131721) | Cod sursa (job #2504965)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
const int Mmax=400005;
pair <int,int> P[Mmax];
int k,n,m,total,tt[Mmax],rg[Mmax];
struct muchie
{
int x,y,cost;
}v[Mmax];
bool compare(muchie a, muchie b)
{
return a.cost<b.cost;
}
void citire()
{
f>>n>>m;
for(int i=1;i<=m;i++)
f>>v[i].x>>v[i].y>>v[i].cost;
sort(v+1,v+m+1,compare);
for(int i=1;i<=m;i++)
{
tt[i]=i;
rg[i]=1;
}
}
void unire(int x, int y)
{
if(rg[x]<rg[y])
tt[x]=y;
if(rg[x]>rg[y])
tt[y]=x;
if(rg[x]==rg[y])
{
tt[x]=y;
rg[y]++;
}
}
int Find(int nod)
{
while(tt[nod]!=nod)
nod=tt[nod];
return nod;
}
void rezolva()
{
for(int i=1;i<=m;i++)
{
int tatal_x=Find(v[i].x);
int tatal_y=Find(v[i].y);
if(Find(v[i].x)!=Find(v[i].y))
{
unire(Find(v[i].x),Find(v[i].y));
P[++k].first=v[i].x;
P[k].second=v[i].y;
total=total+v[i].cost;
}
}
}
void afisare()
{
g<<total<<'\n';
g<<n-1<<'\n';
for(int i=1;i<=k;i++)
g<<P[i].first<<" "<<P[i].second<<'\n';
}
int main()
{
citire();
rezolva();
afisare();
return 0;
}