Cod sursa(job #3368811)

Utilizator noemienoemie noemie Data 15 octombrie 2026 21:35:16
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb

#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream cin("apm.in");
ofstream cout("apm.out");
int n, m;
int sef[200000];
vector <int> much;
vector <int> apm;
vector <int> poz;
vector <int> nod1;
vector <int> nod2;
int sef_suprem(int nod) {
    if (sef[nod]==nod)
        return nod;
    return sef[nod]=sef_suprem(sef[nod]);
}


void unire(int x, int y) {
    int sefx=sef_suprem(x);
    int sefy=sef_suprem(y);
    if (sefx!=sefy) {
        sef[sefx]=sefy;
    }
}
int main()
{   int cost, n1, n2;
    cin>>n>>m;
    for(int i=1; i<=m; i++) {
        cin>>cost>>n1>>n2;
        much.push_back({cost,n1,n2});
        poz[i]=i;}

sort(much.begin(), much.end());

    return 0;
    for(int i=1; i<=n; i++)
        sef[i]=i;
    int suma=0;
    int muchii=0;
    for(int i=1; i<=m && muchii<n-1; i++)
    {    int unde=poz[i];
        int x=much[unde];
        int y=nod1[unde];
        int z=nod2[unde];

        if(sef_suprem(x)!=sef_suprem(y))
          {
              unire(x,y);
            suma+=unde;
            apm.push_back(x);
            apm.push_back(y);
            muchii++;
          }

    }
    cout<<suma<<endl;
    cout<<muchii<<endl;
    for(int i=1; i<=apm.size(); i++)
        cout<<apm[i]<<' '<<apm[i+1]<<endl;
    return 0;
}