Cod sursa(job #2864346)

Utilizator elenacurecheriuElena Curecheriu elenacurecheriu Data 7 martie 2022 19:53:05
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

struct muchie
{
    int x, y, c;
} v[400005];

int n, m, x, y;
int ans, nr;
int cc[400005];
bool used[400005];

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

int conex (int x)
{
    if(cc[x]==x)
        return x;
    cc[x] = conex(cc[x]);
    return cc[x];
}

void unificare (int x, int y)
{
    cc[conex(x)]=conex(y);
}

int main()
{
    fin>>n>>m;
    for(int i=1; i<=m; i++)
    {
        fin>>v[i].x>>v[i].y>>v[i].c;
        cc[i]=i;
    }
    sort(v+1,v+m+1,cmp);
    for(int i=1; i<=m; i++)
    {
        if(conex(v[i].x) != conex(v[i].y))
        {
            ans+=v[i].c;
            used[i]=1;
            nr++;
        }
        unificare(cc[v[i].x], cc[v[i].y]);
    }
    fout<<ans<<'\n'<<nr<<'\n';
    for(int j=1; j<=m; j++)
        if(used[j]==1)
            fout<<v[j].x<<" "<<v[j].y<<'\n';
    return 0;
}