Pagini recente » Cod sursa (job #2065770) | Cod sursa (job #1882632) | Cod sursa (job #770053) | Cod sursa (job #2472967) | Cod sursa (job #2868538)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
const int N=2e5+1;
int n,m,t[N],nr[N];
bool vf[N];
vector <pair<int,int>> sol;
struct edge{
int x,y,c;
}a[N];
bool cmp(edge a, edge b){
return a.c<b.c;
}
int find(int x){
if(t[x]==0)
return x;
t[x]=find(t[x]);
return t[x];
}
void Union (int x, int y){
int tx=find(x);
int ty=find(y);
if(nr[tx]>=nr[ty]){
t[ty]=tx;
nr[tx]+=nr[ty];
}else{
t[tx]=ty;
nr[ty]+=nr[tx];
}
}
int main()
{
f>>n>>m;
for(int i=1; i<=m; i++){
f>>a[i].x>>a[i].y>>a[i].c;
}
sort(a+1,a+m+1,cmp);
int cost=0,nrm=0,i=1;
while(i<=m && nrm<n-1){
if(find(a[i].x)!=find(a[i].y)){
vf[i]=true;
cost+=a[i].c;
Union(a[i].x, a[i].y);
sol.push_back({a[i].x,a[i].y});
nrm++;
}
i++;
}
g<<cost<<'\n'<<sol.size()<<'\n';
for(auto j:sol)
g<<j.first<<" "<<j.second<<'\n';
return 0;
}