Cod sursa(job #3197532)

Utilizator matei__bBenchea Matei matei__b Data 27 ianuarie 2024 09:29:32
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 20'011
#define dim 100005
#define lim 1000000
#define INF 1000000000
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define piii pair<int,pair<int,int> > 
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace std;
 
ifstream fin("apm.in");
ofstream fout("apm.out");

int n,m;
int t[2*dim];

int root(int nod)
{
    if(nod==t[nod])
        return nod;

    return t[nod]=root(t[nod]);
}

struct muchie 
{
    int x,y,w;

    bool operator<(const muchie &other)
    {
        return w<other.w;
    }

}v[4*dim];

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr); 

    fin >> n >> m;

    for(int i=1; i<=n; i++)
        t[i]=i;

    for(int i=1; i<=m; i++)
        fin >> v[i].x >> v[i].y >> v[i].w;

    sort(v+1,v+1+m);

    int ans=0;

    vector<pii> sol;

    for(int i=1; i<=m; i++)
    {
        int rx=root(v[i].x);
        int ry=root(v[i].y);

        if(rx==ry)
            continue;

        t[ry]=rx;
        ans+=v[i].w;

        sol.pb(mp(v[i].x,v[i].y));
    }

    fout << ans << "\n";

    fout << sol.size() << "\n";

    for(auto it:sol)
        fout << it.first << " " << it.second << "\n";

    return 0;
}