Pagini recente » Cod sursa (job #613006) | Cod sursa (job #3039512) | Cod sursa (job #1556793) | Cod sursa (job #2295677) | Cod sursa (job #1663234)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
const int nmax = 2e5+5;
const int mmax = 4e5+5;
vector <pair<int,int> > apm;
int n, m, cmin, dady[nmax], rang[nmax];
struct edge{
int x, y, cost;
inline bool operator () (const edge &x, const edge &y){
return x.cost < y.cost;
}
} v[mmax];
int Find(int node){
while(dady[node]!=node)
node=dady[node];
return node;
}
void Union(int x, int y){
if(rang[x] > rang[y]) dady[y]=x;
else dady[x]=y;
if(rang[x]==rang[y]) rang[y]++;
}
void Kruskal(){
int rx, ry, i;
for(i=1; i<=n; i++)
dady[i]=i;
sort(v+1, v+m+1, edge());
for(i=1; i<=m; i++){
rx=Find(v[i].x);
ry=Find(v[i].y);
if(rx!=ry){
cmin+=v[i].cost;
apm.push_back({v[i].x, v[i].y});
Union(rx, ry);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
int i;
fin >> n >> m;
for(i=1; i<=m; i++){
fin >> v[i].x >> v[i].y >> v[i].cost;
}
Kruskal();
fout << cmin << "\n" << n-1 << "\n";
for(i=0; i<apm.size(); i++)
fout << apm[i].first << " " << apm[i].second << "\n";
fin.close();
fout.close();
return 0;
}