Pagini recente » Cod sursa (job #1829124) | Cod sursa (job #819520) | Cod sursa (job #881885) | Cod sursa (job #2750173) | Cod sursa (job #2236395)
#include <fstream>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
const int N = 100010;
int n,m,i,c,x,y,rx,ry,t[N],cost;
int get_root(int);
vector<tuple<int,int,int>> M;
vector<pair<int,int>> apm;
int main()
{
f>>n>>m;
for(;m;m--)
{
f>>x>>y>>c;
M.push_back(make_tuple(c,x,y));
}
sort(M.begin(),M.end());
for(i=1;i<=n;i++)
t[i]=i;
for(auto it:M)
{
tie(c,x,y)=it;
rx=get_root(x);
ry=get_root(y);
if(rx!=ry)
{
t[rx]=ry;
cost+=c;
apm.push_back(make_pair(x,y));
}
}
g<<cost<<'\n';
g<<apm.size()<<'\n';
for(auto it:apm)
g<<it.first<<' '<<it.second<<'\n';
return 0;
}
int get_root(int x)
{
if(t[x]==x)
return x;
t[x]=get_root(t[x]);
return t[x];
}