Cod sursa(job #3323162)

Utilizator tudorbeloiuLuka Modric tudorbeloiu Data 17 noiembrie 2025 13:00:15
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.77 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
#include <ctype.h>
#include <algorithm>

using namespace std;

ofstream g("apm.out");


class InParser{
    private:
        FILE *file_in;
        char *buff;
        int ebp;

        int read_ch(){
            ++ebp;
            if(ebp == 4096){
                ebp = 0;
                fread(buff, 1, 4096, file_in);
            }
            return buff[ebp];
        }

    public:
        InParser(const char* file_name){
            file_in = fopen(file_name, "r");
            buff = new char[4096];
            ebp = 4095;
        }

        template<typename T, typename = typename enable_if<is_integral<T>::value>::type>
        InParser& operator>>(T& n){
            char ch;
            while(!isdigit(ch = read_ch()) && ch != '-');

            long long sgn = 1;
            long long acc;

            if(ch == '-'){
                sgn = -1;
                acc = 0;
            }
            else{
                acc = ch - '0';
            }
            
            while(isdigit(ch = read_ch())){
                acc = acc * 10 + (ch - '0');
            }

            n = static_cast<T>(acc * sgn);
            return *this;

        }

        ~InParser(){
            fclose(file_in);
            delete[] buff;
        }
};

struct Muchie{
    int x,y,c;
    Muchie(int _x, int _y, int _c) : x(_x), y(_y), c(_c){

    }
};

int n,m,x,y,c;

vector<Muchie*> muchii;
vector<int> tata;
vector<int> rang;

int getTata(int nod){
    if(tata[nod]!=nod){
        tata[nod] = getTata(tata[nod]);
    }
    return tata[nod];
}

void unirea_Romaniei(int x, int y){
    if(rang[x] < rang[y]){
        tata[x] = y;
    }
    if(rang[x] > rang[y]){
        tata[y] = x;
    }
    if(rang[x]==rang[y]){
        tata[x] = y;
        rang[y]++;
    }
}

void kruskal(vector<Muchie*>& muchii){
    vector<Muchie*> muchiiFinale;
    long long total = 0;
    for(auto *m: muchii){
        int tataX = getTata(m->x);
        int tataY = getTata(m->y);

        if(tataX != tataY){
            unirea_Romaniei(tataX,tataY);
            muchiiFinale.push_back(m);
            total += m->c;
        }
    }
    g << total << '\n';
    g << muchiiFinale.size() << '\n';
    for(auto *muchie: muchiiFinale){
        g << muchie->x << " " << muchie->y << '\n';
    }
}
int main(){
    InParser f("apm.in");
    f >> n >> m;
    tata.resize(n+1);
    rang.resize(n+1);
    for(int i=0; i<m; i++){
        f >> x >> y >> c;
        muchii.push_back(new Muchie(x,y,c));
    }
    for(int i=1; i<=n; i++){
        tata[i]=i;
        rang[i]=0;
    }
    sort(muchii.begin(), muchii.end(), [](Muchie *a, Muchie *b){
        return a->c < b->c;
    });

    kruskal(muchii);

    for(auto *muchie: muchii){
        delete muchie;
    }

    return 0;
}