Cod sursa(job #1418272)

Utilizator felixiPuscasu Felix felixi Data 12 aprilie 2015 15:58:23
Problema Lazy Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

ifstream in("lazy.in");
ofstream out("lazy.out");

typedef long long I64;
const I64 NMAX = 200000;

struct MUC {
    I64 x,y,c,b,i;
};

bool cmp( MUC A, MUC B ) {
    return ( A.c < B.c || ( A.c==B.c && A.b > B.b ) );
}

MUC v[NMAX+2];
I64 N, M, tt[NMAX+2], gr[NMAX+2];

I64 Root( I64 nod ) {
    if( tt[nod] == nod ) return nod;
    return ( tt[nod] = Root( tt[nod] ) );
}

void Union( I64 x, I64 y ) {
    I64 Rx = Root(x), Ry = Root(y);
    if( gr[Rx] < gr[Ry] ) {
        tt[Rx] = Ry;
        gr[Ry] += gr[Rx];
    }
    else {
        tt[Ry] = Rx;
        gr[Rx] += gr[Ry];
    }
}

void Next( I64 &nr ) {
    nr = 0;
    char c;
    in.get(c);
    while( c < 48 || c > 58 )  in.get(c);
    while( c > 47 && c < 59 ) {
        nr = nr * 10 + c - 48;
        in.get(c);
    }
}

int main() {
    in >> N >> M;
    for( I64 i = 1;  i <= N;  ++i ) { tt[i] = i;  gr[i] = 1; }
    for( I64 i = 1;  i <= M;  ++i ) {
        Next( v[i].x );
        Next( v[i].y );
        Next( v[i].c );
        Next( v[i].b );

        v[i].i = i;
    }
    sort( v+1, v+M+1, cmp );
    for( I64 i = 1;  i <= M;  ++i ) {
        if( Root( v[i].x ) != Root( v[i].y ) ) {
            out << v[i].i << '\n';
            Union( v[i].x, v[i].y );
        }
    }
    return 0;
}