Cod sursa(job #1122726)

Utilizator dragangabrielDragan Andrei Gabriel dragangabriel Data 25 februarie 2014 20:07:48
Problema Lazy Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include<cstdio>
#include<algorithm>
#define nmax 200005
using namespace std;
struct nod
{
    int x;
    int y;
    int ind;
    long long cost;
    long long profit;
}v[nmax];
int n,i,j,k,tata[nmax],a,b,rez,m;

int cmp(const nod a,const nod b)
{
    if (a.cost==b.cost)
        return a.profit<b.profit;
    return a.cost<b.cost;
}

int find(int x)
{
    if (!tata[x]) return x;
    return find(tata[x]);
}

int main()
{
    freopen("lazy.in","r",stdin);
    freopen("lazy.out","w",stdout);
    scanf("%d %d",&n,&m);
    for (i=1;i<=m;i++)
    {
        scanf("%d %d %lld %lld",&v[i].x,&v[i].y,&v[i].cost,&v[i].profit);
        v[i].ind=i;
    }
    sort(v+1,v+n+1,cmp);
    for (i=1;i<=m && n;i++)
    {
        a=find(v[i].x);
        b=find(v[i].y);
        if (a!=b)
        {
            printf("%d ",v[i].ind);
            n--;
            if (tata[a]>tata[b])
                tata[b]=a;else
                    tata[a]=b;
        }
    }
    return 0;
}