Cod sursa(job #3215682)

Utilizator maiaauUngureanu Maia maiaau Data 15 martie 2024 11:41:49
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define pb push_back
#define fi first 
#define se second

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

const int N = 1e5+3;

int n, m, comp[N], getroot(int);
vector<pii> v, e; //cst, ord / a, b
vector<int> r;

//r - cost, ord muchie

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= n; i++) comp[i] = i;
    for (int i = 0; i < m; i++){
        int a, b, c; fin >> a >> b >> c;
        e.pb({a, b}); v.pb({c, i});
    }
    sort(v.begin(), v.end());
    int cntc = n-1, cst = 0, k = 0;
    while (cntc) {
        int c, which; tie(c, which) = v[k];
        int a, b; tie(a, b) = e[which];
        int x = getroot(a), y = getroot(b);
        if (x != y){
            cntc--; comp[x] = y;
            cst += c;
            r.pb(k);
        }
        k++;
    }
    fout << cst << '\n' << r.size() << '\n';
    for (auto it: r)
        fout  << e[v[it].se].fi << ' ' << e[v[it].se].se << '\n';

    return 0;
}

int getroot(int nod){
    if (comp[nod] == nod) return nod;
    return (comp[nod] = getroot(comp[nod]));
}


//11:30