Cod sursa(job #1122806)

Utilizator dragangabrielDragan Andrei Gabriel dragangabriel Data 25 februarie 2014 20:35:07
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include<cstdio>
#include<algorithm>
#include<vector>
#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;
vector<int>sol;

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+m+1,cmp);
    for (i=1;i<=m && rez<n-1;i++)
    {
        a=find(v[i].x);
        b=find(v[i].y);
        if (a!=b)
        {
            sol.push_back(v[i].ind);
            rez++;
            if (a>b)
                tata[b]=a;else
                    tata[a]=b;
        }
    }
    sort(sol.begin(),sol.end());
    for (vector<int>:: iterator it=sol.begin(); it!=sol.end();++it)
        printf("%d\n",*it);
    return 0;
}