Pagini recente » Cod sursa (job #71440) | Cod sursa (job #227154) | Cod sursa (job #1778144) | Cod sursa (job #541239) | Cod sursa (job #3192249)
#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;
signed short int cost;
bool operator < (const muchie &other) const
{
return (cost <= other.cost);
}
};
muchie v[mmax];
vector<muchie> apm;
int tata[nmax], h[mmax], 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;
apm.push_back({crt.x,crt.y,crt.cost});
if(h[rx]==h[ry])
h[ry]++;
if(h[rx]>h[ry])
tata[ry]=rx;
else
tata[rx]=ry;
}
}
int main()
{
cin>>n>>m;
for(int i=1; i<=m; i++)
{
cin>>v[i].x>>v[i].y>>v[i].cost;
}
memset(tata,0,sizeof(tata));
sort(v+1,v+m+1);
for(int i=1; i<=m; i++)
{
unire(v[i]);
}
cout<<sum<<endl<<apm.size()<<endl;
for(auto mcrt : apm)
cout<<mcrt.x<<' '<<mcrt.y<<endl;
}