Cod sursa(job #2677399)

Utilizator bogdan62003isache bogdan bogdan62003 Data 26 noiembrie 2020 15:06:27
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>
#define NM 200001
#define MM 400001

using namespace std;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
struct muchie
{
    int x,y,c,ok;
}v[MM];

int n,m,i,t[NM];

int tata(int x)
{
    int r, y, aux;
    r=x;
    while(t[r]!=0)
        r=t[r];
    ///compresia drumurilor
    y=x;
    while(t[y]!=0)
    {
        aux=t[y];
        t[y]=r;
        y=aux;
    }
    return r;
}

bool comp(muchie u, muchie v)
{
    if(u.c<v.c)
        return 1;
    return 0;
}

int main()
{
    fin>>n>>m;
    for(i=1;i<=m;i++)
        fin>>v[i].x>>v[i].y>>v[i].c;
    int j;
    sort(v+1, v+m+1, comp);
    for(i=1;i<=n;i++)
        t[i]=0;
   int ct=0;
   int nr=0;
   i=1;
   int x,y,tx,ty;
    while(nr<n-1)
    {
        x=v[i].x;
        y=v[i].y;
        tx=tata(x);
        ty=tata(y);
        if(tx!=ty)
        {
            nr++;
            v[i].ok=1;
            ct=ct+v[i].c;
            t[ty]=tx;
        }
        i++;
    }
    fout<<ct<<'\n'<<n-1<<'\n';
    for(i=1;i<=m;i++)
        if(v[i].ok==1)
            fout << v[i].x<<' '<<v[i].y<<'\n';
    return 0;
}