Cod sursa(job #545113)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 2 martie 2011 18:38:06
Problema Lazy Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <stdio.h>
#include <algorithm>
#define Nmax 200002
using namespace std;

struct muchie{
    int a,b,c1,c2;
    inline bool operator < ( const muchie &o ) const{
        return c1 == o.c1 ? ( c2>o.c2 ) : c1<o.c1;
    }
} V[Nmax];

int TT[Nmax],Rg[Nmax],ind[Nmax];
int N,M;

inline int cmp(int i,int j){
    return V[i] < V[j];
}

inline int Find(int x){
    int r,y;
    for(r=x; r!=TT[r]; r=TT[r]);

    while( x!=TT[x] ){
        y=TT[x];
        TT[x]=r;
        x=y;
    }
    return r;
}
inline void Merge(int x,int y){
    if( Rg[x] > Rg[y] ) TT[y]=x;
    else TT[x]=y;

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

int main(){
    int i,t1,t2;
    freopen("lazy.in","r",stdin);
    freopen("lazy.out","w",stdout);
    scanf("%d%d",&N,&M);
    for(i=1;i<=M;++i){
        scanf("%d%d%d%d",&V[i].a,&V[i].b,&V[i].c1,&V[i].c2);
        TT[i]=i; Rg[i]=1;
        ind[i]=i;
    }
    sort(ind+1,ind+M+1,cmp);

    for(i=1;i<=M;++i){
        t1=Find(V[ind[i]].a);
        t2=Find(V[ind[i]].b);

        if(t1!=t2){
            Merge(t1,t2);
            printf("%d\n",ind[i]);
        }
    }

    fclose(stdin); fclose(stdout);
    return 0;
}