Pagini recente » Cod sursa (job #1160389) | Cod sursa (job #1880638) | Cod sursa (job #2873604) | Cod sursa (job #2975595) | Cod sursa (job #2868998)
#include <bits/stdc++.h>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
const int N = 200010;
int n,m,t[N],costApm;
vector<tuple<int,int,int>> E;
vector<pair<int,int>> sol;
int getRoot(int);
int main()
{
f>>n>>m;
for(int i=1;i<=n;i++)
t[i]=i;
for(;m;m--)
{
int cost,x,y;
f>>x>>y>>cost;
E.push_back(make_tuple(cost,x,y));
}
sort(E.begin(),E.end());
for(auto it:E)
{
int x,y,cost,rx,ry;
tie(cost,x,y)=it;
rx=getRoot(x);
ry=getRoot(y);
if(rx!=ry)
{
t[rx]=ry;
costApm+=cost;
sol.push_back(make_pair(x,y));
}
}
g<<costApm<<'\n'<<n-1<<'\n';
for(auto it:sol)
g<<it.first<<' '<<it.second<<'\n';
return 0;
}
int getRoot(int nod)
{
if(t[nod]==nod)
return nod;
t[nod]=getRoot(t[nod]);
return t[nod];
}