Cod sursa(job #1248351)

Utilizator xtreme77Patrick Sava xtreme77 Data 24 octombrie 2014 22:51:23
Problema Lazy Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
/*
 * Code by Spiromanul
 */


#include <fstream>
#include <algorithm>
#include <vector>

const char IN [ ] = "lazy.in" ;
const char OUT [ ] = "lazy.out" ;
const int  MAX = 200014 ;

using namespace std;

ifstream fin ( IN ) ;
ofstream fout ( OUT ) ;

int  tata [ MAX ] , rang [ MAX ] ;

struct nationala {
    int  a ; int  b ;
    long long  c1 , c2 ;
    int  poz ;
    void sett ( int  aaa , int  bbb , long long  ccc , long long  ddd , int  eee )
    {
        a = aaa ;
        b = bbb ;
        c1 = ccc ;
        c2 = ddd ;
        poz = eee ;
    }
};

nationala q [ MAX ] ;

bool cmp ( nationala a , nationala b )
{
    if ( a.c1 == b.c1 )
        return a.c2 > b.c2 ;
    return a.c1 < b.c1 ;
}

int  stramos ( int  nod )
{
    int  R = nod ;
    for ( ; tata [ R ] != R ; R = tata [ R ] ) ;
    while ( nod != tata [ nod ] )
    {
        int  aux = tata [ nod ] ;
        tata [ nod ] = R ;
        nod = aux ;
    }
    return R ;
}
void uneste ( int  a , int  b )
{
    if ( rang [ a ] < rang [ b ] )
        swap ( a , b ) ;
    tata [ b ] = a ;
    rang [ a ] += rang [ b ] ;
}

int main(              )
{
    int  n , m ;
    int  a , b ;
    long long c , d  ;
    fin >> n >> m ;
    for ( int  i = 1 ; i <= n ; ++ i )
    {
        tata [ i ] = i ;
        rang [ i ] = 1 ;
    }
    for ( int  i = 1 ; i <= m ; ++ i )
    {
        fin >> a >> b >> c >> d ;
        q [ i ].sett ( a , b , c , d , i ) ;
    }
    sort ( q + 1 , q + m + 1 , cmp ) ;
    for ( int  i = 1 ; i <= m ; ++ i )
        if ( stramos ( q [ i ].a ) != stramos ( q [ i ].b ) ){
            uneste ( stramos ( q [ i ].a ) , stramos ( q [ i ].b ) ) ;
            fout << q [ i ].poz << '\n' ;
        }
    return 0;
}