Cod sursa(job #2651469)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 22 septembrie 2020 18:24:15
Problema Lazy Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define f in
#define g out

using namespace std;
ifstream in ( "lazy.in" );
ofstream out( "lazy.out" );
struct potae{
    int x, y, poz;
    long long c1, c2;
} v[200200];
vector<int> sol;
int n, m, i, t[200200];

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

int rad ( int x ){
    while ( t[x] > 0 )
        x = t[x];
    return x;
}

int main() {
    
    f>>n>>m;
    for ( i=1; i <= m; i++ ){
        f>>v[i].x>>v[i].y>>v[i].c1>>v[i].c2;
        v[i].poz = i;
    }
    
    sort( v+1, v+m+1, cmp);
    for ( i=1; i <= n; i++ )
        t[i] = -1;
    
    for ( i=1; i <= m && sol.size() < n; i++ ){
        int rx = rad ( v[i].x );
        int ry = rad ( v[i].y );
        if ( rx == ry ) continue;
        if ( t[rx] < t[ry] ){
            t[rx] += t[ry];
            t[ry] = rx;
        } else {
            t[ry] += t[rx];
            t[rx] = ry;
        }
        sol.push_back(v[i].poz);
    }
    for( auto x: sol )
        g<<x<<"\n";
    return 0;
}