Cod sursa(job #1928631)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 16 martie 2017 16:16:35
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>
#define z(x) (x & (-x))
typedef long long ll;
using namespace std;

ll sum, n, m, p[200100];
struct my{int x, y, c;} A[200100];
int find(int a){return (p[a] == a ? a : p[a] = find(p[a]));}
void unite(int a, int b){p[find(b)] = find(a);}
bool cmp(my a, my b){return a.c < b.c;}
vector <int> V;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ifstream cin("apm.in");
    ofstream cout("apm.out");
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> A[i].x >> A[i].y >> A[i].c;
    for (int i = 1; i <= n; i++) p[i] = i;
    sort(A+1, A+m+1, cmp);
    for (int i = 1; i <= m; i++) if(find(A[i].x) != find(A[i].y)) sum += A[i].c, V.push_back(i), unite(A[i].x, A[i].y);
    cout << sum << "\n" << V.size() << "\n";
    for (auto i : V) cout << A[V[i]].x << " " << A[V[i]].y << "\n";
    return 0;
}