Cod sursa(job #541754)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 25 februarie 2011 13:57:50
Problema Lazy Scor 100
Compilator cpp Status done
Runda Romanian Master in Mathematics and Sciences 2011, Ziua 1 Marime 1.83 kb
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

#define id first
#define x second.first.first
#define y second.first.second
#define c1 second.second.first
#define c2 second.second.second

const char Input[] = "lazy.in";
const char Output[] = "lazy.out";

const int Dim = 200001;

int N, M;
int r[Dim], t[Dim];
vector < pair < int, pair < pair <int, int>, pair <long long int, long long int> > > > m;

struct cmp {

    bool operator()( pair < int, pair < pair <int, int>, pair <long long int, long long int> > > a, pair < int, pair < pair <int, int>, pair <long long int, long long int> > > b ) {

        return a.c1 < b.c1 || ( a.c1 == b.c1 && a.c2 > b.c2 );
    }
};

int Find( int a ) {

    if( a != t[a] )
        t[a] = Find( t[a] );

    return t[a];
}

void Unite( int a, int b ) {

    if( r[a] < r[b] )
        t[a] = b;
    else
        t[b] = a;

    if( r[a] == r[b] )
        ++r[a];
}

int main() {

    ifstream fin( Input );
    ofstream fout( Output );

    int i, a, b, t1, t2;
    long long c, d;
    vector < pair < int, pair < pair <int, int>, pair <long long int, long long int> > > > :: iterator it;

    fin >> N >> M;
    for( i = 1; i <= M; ++i ) {

        fin >> a >> b >> c >> d;
        m.push_back( make_pair( i, make_pair( make_pair( a, b ), make_pair( c, d ) ) ) );
    }

    sort( m.begin(), m.end(), cmp() );

//    for( it = m.begin(); it != m.end(); ++it )
//        fout << it->id << "\n";

    for( i = 1; i <= N; ++i )
        t[i] = i;

    for( it = m.begin(); it != m.end(); ++it ) {

        t1 = Find( it->x );
        t2 = Find( it->y );

        if( t1 != t2 ) {

            Unite( t1, t2 );
            fout << it->id << "\n";
        }
    }

    fin.close();
    fout.close();

    return 0;
}