Cod sursa(job #2785021)

Utilizator Tudor06MusatTudor Tudor06 Data 17 octombrie 2021 20:36:17
Problema Politie Scor 0
Compilator cpp-64 Status done
Runda Arhiva ICPC Marime 1.37 kb
#include <bits/stdc++.h>
#include <fstream>


using namespace std;

const int NMAX = 250000;
const int MMAX = 500000w;

struct strada {
    int p;
    int c;
    int x;
    int y;
} edges[MMAX + 1];
int father[NMAX + 1];

int root( int x ) {
    if ( father[x] == x )
        return x;
    return ( father[x] = root( father[x] ) );
}
vector <int> ans;

void unire( strada a ) {
    int rootx = root( a.x );
    int rooty = root( a.y );
    if ( rootx != rooty ) {
        ans.push_back( a.p );
        father[rootx] = rooty;
    }

}
bool cmp( strada a, strada b ) {
    return ( a.c < b.c || ( a.c == b.c && a.p < b.p ) );
}
ifstream fin( "politie.in" );
ofstream fout( "politie.out" );

int main() {
    int n, m, p, d, i;
    fin >> n >> m >> d >> p;
    for ( i = 1; i <= m; i ++ ) {
        father[i] = i;
        int x, y, c, pericol;
        fin >> x >> y >> c >> pericol;
        edges[i] = { pericol, c, x, y };
    }
    sort( edges + 1, edges + m + 1, cmp );
    for ( i = 1; i <= m; i ++ ) {
        unire( edges[i] );
    }
    sort( ans.begin(), ans.end() );

    fout << ans[ans.size() - 1] << '\n';
    p --;
    for ( i = ans.size() - 2; i >= 0; i -- ) {
        ///fout << ans[i] << ' ';
        if ( p > 0 && ans[i] != ans[i + 1] ) {
            fout << ans[i] << '\n';
            p --;
        }
    }
    return 0;
}