Pagini recente » Cod sursa (job #1101339) | Cod sursa (job #2278735) | Cod sursa (job #2132450) | Cod sursa (job #2895254) | Cod sursa (job #3192251)
#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], 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(tata[rx]>tata[ry])
swap(rx,ry);
tata[ry]=rx;
}
}
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<<'\n'<<apm.size()<<'\n';
for(auto mcrt : apm)
cout<<mcrt.x<<' '<<mcrt.y<<'\n';
}