Cod sursa(job #2282327)

Utilizator thed22what the f thed22 Data 13 noiembrie 2018 16:59:58
Problema Arbore partial de cost minim Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include<fstream>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int n,m,v[200001],x[400001],y[400001],c[400001];
long long s;
int main()
{
    f>>n>>m;
    for(int i=1;i<=m;i++)
        f>>x[i]>>y[i]>>c[i];
    for(int i=1;i<m;i++)
    {
        for(int j=i+1;j<=m;j++)
        {
            if(c[i]>c[j])
            {
                swap(x[i],x[j]);
                swap(y[i],y[j]);
                swap(c[i],c[j]);
            }
        }
    }
    int k=0;
    for(int i=1;i<=n;i++) v[i]=i;

    for(int i=1;i<=m&&k<n;i++)
    {
        if(v[x[i]]!=v[y[i]])
        {
            s+=c[i]; int aux2=v[x[i]],aux=v[y[i]]; k++;
            for(int j=1;j<=n;j++)
            {
                if(v[j]==aux) v[j]=aux2;
            }
        }
        else c[i]=-1;
    }
    g<<s<<endl<<n-1<<endl;
    for(int i=1;i<=m;i++)
    {
        if(c[i]!=-1)
            g<<x[i]<<" "<<y[i]<<endl;
    }
    return 0;
}