Cod sursa(job #1693777)

Utilizator CrystyAngelDinu Cristian CrystyAngel Data 23 aprilie 2016 21:07:26
Problema Arbore partial de cost minim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream f("apm.in");
ofstream g("apm.out");

struct muchie
{
    int a,b,c;
};

int t[200100],n,m,i,c;
muchie v[400100];
vector <int> r;

int root(int x)
{
    while(t[x])
        x=t[x];
    return x;
}

bool compare(muchie a,muchie b)
{
    return a.c<b.c;
}

int main()
{
    f>>n>>m;
    for(i=0; i<m; ++i)
        f>>v[i].a>>v[i].b>>v[i].c;

    sort(v,v+m,compare);

    for(i=0; i<m; ++i)
    {
        if(root(v[i].a)!=root(v[i].b))
        {
            c+=v[i].c;
            t[root(v[i].a)]=root(v[i].b);
            r.push_back(i);
        }
    }
    g<<c<<'\n'<<r.size()<<'\n';
    for(i=0; i<r.size(); ++i)
        g<<v[r[i]].a<<' '<<v[r[i]].b<<'\n';
}