Cod sursa(job #1671468)

Utilizator Athena99Anghel Anca Athena99 Data 1 aprilie 2016 19:27:00
Problema Lazy Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <algorithm>
#include <fstream>

using namespace std;

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

typedef long long i64;

const i64 nmax= 200000;

i64 r[nmax+1], edge[nmax+1];

struct str {
    i64 x, y, d, p;
} v[nmax+1];

i64 root( i64 x ) {
    if ( r[x]!=x )
        return r[x]= root(r[x]);
    return r[x];
}

bool comp( i64 i, i64 j ) {
    if ( v[i].d==v[j].d )
        return v[i].p>v[j].d;
    return v[i].d<v[j].d;
}

int main(  ) {
    i64 n, m;
    fin>>n>>m;
    for ( i64 i= 1; i<=m; ++i ) {
        fin>>v[i].x>>v[i].y>>v[i].d>>v[i].p;
        edge[i]= i;
    }
    for ( i64 i= 1; i<=n; ++i ) {
        r[i]= i;
    }

    sort( edge+1, edge+m+1, comp );

    for ( i64 i= 1; i<=m; ++i ) {
        i64 x= root(v[edge[i]].x), y= root(v[edge[i]].y);
        if ( x!=y ) {
            r[y]= r[x];
            fout<<edge[i]<<"\n";
        }
    }

    return 0;
}