Cod sursa(job #2317019)

Utilizator Nistor_Andrei_FMI_UVTNistor Andrei Nistor_Andrei_FMI_UVT Data 12 ianuarie 2019 18:09:26
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <algorithm>
#define MMAX 400004
#define NMAX 200004

using namespace std;

ifstream fin("apm.in");
ofstream gut("apm.out");

int n, m, cc[NMAX], sol[NMAX], ct;

int find (int x){
    if (cc[x] == x)
        return x;
    return cc[x] = find(cc[x]);
}
void ren(int x, int y){
    cc[find(x)] = find(y);
}

struct s{
    int x, y, cost;
    bool operator < (const s &other) const{
        return cost < other.cost;
    }
} v[NMAX];

int main(){
    fin >> n >> m;
    for (int i = 1; i <= m ; i++){
        fin >> v[i].x >> v[i].y >>v[i].cost;
    }
    for (int i = 1; i <= n; i++)
        cc[i] = i;
    sort(v+1, v+m+1);
    for (int i=1; i<=m; i++){
        if (find(v[i].x) != find(v[i].y)){
            ren(v[i].x, v[i].y);
            ct += v[i].cost;
            sol[0]++;
            sol[sol[0]] = i;
        }
    }
    gut << ct << '\n' <<sol[0] << '\n';
    for (int i=1; i<=sol[0]; i++)
        gut << v[sol[i]].x << ' ' << v[sol[i]].y <<'\n';
    fin.close();
    gut.close();
    return 0;
}