Pagini recente » Cod sursa (job #2319870) | Cod sursa (job #543727) | Cod sursa (job #3269791) | Cod sursa (job #218434) | Cod sursa (job #1615867)
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <cstring>
#include <iomanip>
#include <fstream>
#define NMax 400005
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct muchie{
int x,y,c;
}G[NMax];
int n,m,Rx,Ry,cost,muchii;
int r[NMax],sol[NMax];
int cmp(muchie x,muchie y){
return (x.c < y.c);
}
int root(int x){
while(r[x])
x = r[x];
return x;
}
void kruskal(){
for(int i = 1; i <= m && muchii < n - 1; ++i){
Rx = root(G[i].x);
Ry = root(G[i].y);
if(Rx != Ry){
sol[++muchii] = i;
cost += G[i].c;
r[Rx] = Ry;
}
}
}
int main()
{
f >> n >> m;
for(int i = 1; i <= m; ++i){
f >> G[i].x >> G[i].y >> G[i].c;
}
sort(G + 1, G + 1 + m,cmp);
kruskal();
g << cost <<"\n" << n - 1 <<"\n";
for(int i = 1; i < n; ++i){
g << G[sol[i]].x <<" " << G[sol[i]].y <<"\n";
}
return 0;
}