Cod sursa(job #541458)

Utilizator LgregL Greg Lgreg Data 25 februarie 2011 11:34:50
Problema Lazy Scor 100
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.14 kb
#include<stdio.h>
#include<algorithm>
#define Nmax 201010

using namespace std;

struct cel
{
    int ind,x,y;
    long long c1,c2;
}v[Nmax];

bool cmp(cel a, cel b)
{
    if(a.c1==b.c1)
    {
        return a.c2>b.c2;
    }
    else
    return a.c1<b.c1;
}
int tata[201010],tip,h[201010];
int find(int x)
{
int R,y;
    R=x;
    while(tata[R]!=R)
        R=tata[R];
    while(tata[x]!=x)
    {
    y=tata[x];
    tata[x]=R;
    x=y;
    }
    return R;
}
int unite(int x,int y)
{
    if(h[x]>h[y])
    {
        tata[y]=x;
    }
    else tata[x]=y;

    if(h[x]==h[y])
        h[y]++;
}

int N,M;

int main()
{
freopen("lazy.in","r",stdin);
freopen("lazy.out","w",stdout);
    scanf("%d%d",&N,&M);
    for(int i=1;i<=M;++i)
    {
        scanf("%d%d%lld%lld",&v[i].x,&v[i].y,&v[i].c1,&v[i].c2);
        v[i].ind=i;
    }
    sort(v+1,v+M+1,cmp);
    for(int i=1;i<=N;++i)
        {
            tata[i]=i;
        }
    for(int i=1;i<=M;++i)
    {
    if(find(v[i].x)!=find(v[i].y))
        {
            unite(find(v[i].x),find(v[i].y));
            printf("%d\n",v[i].ind);
        }
    }
}