Cod sursa(job #1260552)

Utilizator andreeainfo_dAndreea Dutulescu andreeainfo_d Data 11 noiembrie 2014 13:26:33
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include<cstdio>
#include<algorithm>
using namespace std;
int j,x,y,n,m,s,i,w,t[200002],ww[200002],v[200002];
struct andreea
{
    int n1,n2;
    short val;
}a[400002];
bool cmp(andreea a,andreea b)
{
    if(a.val>=b.val)return 0;
    return 1;
}
int mama(int xx)
{
    if(t[xx]!=xx)t[xx]=mama(t[xx]);
    return t[xx];
}
int main()
{
    freopen("apm.in","r",stdin);
    freopen("apm.out","w",stdout);
    scanf("%d",&n);
    scanf("%d",&m);
    for(i=1;i<=n;i++)
    {
        v[i]=1;
        t[i]=i;
    }
    for(i=1;i<=m;i++)
    {
        scanf("%d %d %d",&a[i].n1,&a[i].n2,&a[i].val);
    }
    sort(a+1,a+m+1,cmp);
    for(i=1;i<=m;i++)
    {
        x=mama(a[i].n1);
        y=mama(a[i].n2);
        if(x!=y)
        {
            if(v[x]<v[y])t[x]=y;
            else t[y]=x;
            if(v[x]==v[y]) v[y]++;
            w++;
            ww[w]=i;
            s=s+a[i].val;
            if(w==n-1)break;
        }
    }
    printf("%d\n%d\n",s,w);
    for(i=1;i<=w;i++)
    {
        printf("%d %d\n",a[ww[i]].n1,a[ww[i]].n2);
    }
    return 0;
}