Cod sursa(job #1962701)

Utilizator RaduToporanRadu Toporan RaduToporan Data 11 aprilie 2017 23:33:20
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>
#include <algorithm>

using namespace std;
struct muchie
{
    int x,y,cost;
} a[400005];
struct muchie2
{
    int x,y;
} sol[400005];
int n,m,i,r1,r2,muchii,costmin,t[200005];

bool cmp(muchie a, muchie b)
{
    return a.cost<b.cost;
}

int tata(int x)
{
    if (x==t[x]) return x;
        else t[x]=tata(t[x]);
    return t[x];
}

int main()
{
    freopen("apm.in","r",stdin);
    freopen("apm.out","w",stdout);
    scanf("%d%d",&n,&m);
    for (i=1; i<=m; i++)
        scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].cost);
    sort(a+1,a+m+1,cmp);
    for (i=1; i<=n; i++)
        t[i]=i;
    for (i=1; i<=m&&muchii<n-1; i++)
    {
        r1=tata(a[i].x);
        r2=tata(a[i].y);
        if (r1!=r2)
        {
            muchii++;
            sol[muchii].x=a[i].x;
            sol[muchii].y=a[i].y;
            costmin=costmin+a[i].cost;
            t[r2]=r1;
        }
    }
    printf("%d\n%d\n",costmin,n-1);
    for (i=1; i<=muchii; i++)
        printf("%d %d\n",sol[i].x,sol[i].y);
    return 0;
}