Pagini recente » Cod sursa (job #1915193) | Cod sursa (job #863082) | Cod sursa (job #1335399) | Cod sursa (job #385085) | Cod sursa (job #3165928)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
const int nmax = 200005;
struct node{
int a, b, cost;
};
bool comp(node m, node p){
return m.cost < p.cost;
}
node v[nmax * 2];
int x[nmax], y[nmax];
int father[nmax];
int n, m;
int gaseste_tatal(int x){
if(father[x] < 0){
return x;
}
int root = gaseste_tatal(father[x]);
father[x] = root;
return root;
}
void unite(int x, int y){
int r_x = gaseste_tatal(x);
int r_y = gaseste_tatal(y);
if(father[r_x] < father[r_y]){
swap(r_x, r_y);
}
father[r_y] += father[r_x];
father[r_x] = r_y;
}
int main(){
f >> n >> m;
int len = 0;
int cost_minim = 0;
for(int i = 1; i <= n; i++){
father[n] = -1;
}
for(int i = 1; i <= m; i++){
f >> v[i].a >> v[i].b >> v[i].cost;
}
sort(v + 1, v + m + 1, comp);
for(int i = 1; i <= m; i++){
int r_a = gaseste_tatal(v[i].a);
int r_b = gaseste_tatal(v[i].b);
if(r_a != r_b){
len++;
x[len] = v[i].a;
y[len] = v[i].b;
cost_minim = v[i].cost;
unite(v[i].a, v[i].b);
}
}
g << cost_minim << '\n' << len << '\n';
for(int i = 1; i <= len; i++){
g << x[i] << ' ' << y[i] << '\n';
}
}