Pagini recente » Cod sursa (job #2895271) | Cod sursa (job #887424) | Cod sursa (job #1954293) | Cod sursa (job #766237) | Cod sursa (job #3192337)
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
#define nmax 200001
#define mmax 400001
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
struct muchie
{
int x,y,cost;
}v[400005];
bool comp(muchie a, muchie b)
{
return a.cost < b.cost;
}
vector<pair<int,int>> sol;
int tata[nmax], h[nmax], n, m, q, sum;
int rad(int x)
{
while(tata[x]>0)
return rad(tata[x]);
return x;
}
void unire(muchie crt)
{
int rx = rad(crt.x);
int ry = rad(crt.y);
if(rx!=ry)
{
sum+=crt.cost;
sol.push_back({crt.x,crt.y});
if(h[rx]<h[ry])
swap(rx,ry);
tata[ry]=rx;
h[rx]+=h[ry];
}
}
int main()
{
cin>>n>>m;
for(int i=1; i<=m; i++)
{
cin>>v[i].y>>v[i].x>>v[i].cost;
}
for(int i=1; i<=n; i++)
tata[i]=-1, h[i]=1;
sort(v+1,v+m+1,comp);
for(int i=1; i<=m; i++)
{
unire(v[i]);
}
cout<<sum<<'\n'<<sol.size()<<'\n';
for(auto mcrt : sol)
cout<<mcrt.first<<' '<<mcrt.second<<'\n';
}