Cod sursa(job #2161219)

Utilizator RaduVFVintila Radu-Florian RaduVF Data 11 martie 2018 16:11:40
Problema Arbore partial de cost minim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");

const int MAXN = 200002;
int tata[MAXN];
int cost=0,n,m;
struct Muchie {
    int x, y, cost;
}G[MAXN*2];
vector <int> sol;

int root(int nod) {
    if (tata[nod] == 0)
        return nod;
    return tata[nod] = root(tata[nod]);
}

bool cmp(Muchie a, Muchie b) {
    return a.cost < b.cost;
}

int main() {
    fin>>n>>m;
    for(int i=0; i<m; ++i) {
        fin>>G[i].x>>G[i].y>>G[i].cost;
    }
    sort(G, G+m, cmp);
    for (int i=0; i<m; ++i) {
        if (root(G[i].x) != root(G[i].y)) {
            tata[root(G[i].x)] = root(G[i].y);
            sol.push_back(i);
            cost += G[i].cost;
        }
    }
    fout<<cost<<endl<<sol.size()<<endl;
    for (unsigned i=0; i<sol.size(); ++i)
        fout<<G[i].x << ' '<<G[i].y<<endl;
}