Cod sursa(job #708423)

Utilizator MagnvsDaniel Constantin Anghel Magnvs Data 6 martie 2012 20:02:19
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <cstdio>
#include <algorithm>
#include <stack>
#include <vector>

using namespace std;

typedef long long i64;

struct str
{
    int a,b,i;
    i64 x,y;
};

struct str_cmp
{
    bool operator()(str x,str y)
    {
        if (x.x==y.x)
            return x.y>y.y;
        else
            return x.x<y.x;
    };
};

const int N_MAX=200000;

int r[N_MAX+1],sol[N_MAX];
stack <int> s;
str e[N_MAX+1];

void upd_r(int x)
{
    while (r[r[x]]!=r[x])
    {
        s.push(x);
        x=r[x];
    }

    while (!s.empty())
    {
        r[s.top()]=r[x];
        s.pop();
    }
}

int main()
{
    int n,m,i,x,y;

    freopen("lazy.in","r",stdin);
    freopen("lazy.out","w",stdout);

    scanf("%d%d",&n,&m);
    for (i=1;i<=m;++i)
    {
        e[i].i=i;
        scanf("%d%d%lld%lld",&e[i].a,&e[i].b,&e[i].x,&e[i].y);
    }
    sort(e+1,e+m+1,str_cmp());

    for (i=1;i<=n;++i)
        r[i]=i;

    for (i=1;i<=m;++i)
    {
        x=e[i].a,y=e[i].b;

        upd_r(x);
        upd_r(y);

        if (r[x]!=r[y])
        {
            printf("%d\n",e[i].i);
            r[r[x]]=r[y];
        }
    }

    return 0;
}