Cod sursa(job #2860168)

Utilizator Alle43221Moroz Alexandra-Ioana Alle43221 Data 2 martie 2022 11:51:59
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

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

int T[200001];
int n, m, x, y, c, tx, ty;
int S, M;

struct Muchie{
    int c, a, b;
}aux;

struct Compare{
    bool operator()(Muchie const& p1, Muchie const& p2)
    {
        return p1.c > p2.c;
    }
};

priority_queue <Muchie, vector <Muchie>, Compare> Q;
vector <Muchie> V;

int det_tata(int x)
{
    if(T[x]==0)
    {
        return x;
    }
    T[x]=det_tata(T[x]);
    return T[x];
}

int main()
{
    fin>>n>>m;
    for(int i=0; i<m; i++)
    {
        fin>>aux.a>>aux.b>>aux.c;
        Q.push(aux);
    }
    while(!Q.empty())
    {
        aux=Q.top();
        tx=det_tata(aux.a);
        ty=det_tata(aux.b);
        if(tx!=ty)
        {
            S+=aux.c;
            T[tx]=ty;
            M++;
            V.push_back(aux);
        }
        Q.pop();
    }
    fout<<S<<'\n'<<M<<'\n';
    for(auto i: V)
    {
        fout<<i.b<<" "<<i.a<<'\n';
    }
    fin.close();
    fout.close();
    return 0;
}