Cod sursa(job #1710126)

Utilizator UPB_Darius_Rares_SilviuPeace my pants UPB_Darius_Rares_Silviu Data 28 mai 2016 16:04:09
Problema Politie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.52 kb
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#define NMAX 250009
using namespace std;

struct edge {
    int x, y, c, p;
    edge () {}
    edge (int _x, int _y, int _c, int _p) : x(_x), y(_y), c(_c), p(_p) {}

    bool operator<(const edge& e) const {
        if (c != e.c) {
            return c < e.c;
        }
        return p < e.p;
    }
};

int N, M, D, P, cost, T[ NMAX ];
vector< edge > E;
vector<int> sol;

int djset(int x) {
    if (T[ x ] != x) {
        T[ x ] = djset( T[ x ] );
    }
    return T[ x ];
}

int reunion(int x, int y) {
    T[ djset(x) ] = djset( y );
}

template<class T>
void makeUnique(vector<T> &v){
  sort(v.begin(), v.end());
  v.erase( unique( v.begin(), v.end() ), v.end());
}


int main() {
    freopen("politie.in", "r", stdin);
    freopen("politie.out", "w", stdout);

    scanf("%d%d%d%d", &N, &M, &D, &P);
    while (M--) {
        int x, y, c, p;
        scanf("%d%d%d%d", &x, &y, &c, &p);
        E.push_back( edge(x, y, c, p) );
    }

    sort(E.begin(), E.end());

    for (int i = 1; i <= N; ++i) {
        T[ i ] = i;
    }

    for (vector<edge>::iterator it =  E.begin(); it != E.end(); ++it) {
        int x = it->x, y = it->y, c = it->c;
        if (djset(x) != djset(y)) {
            reunion(x, y);
            sol.push_back( it->p );
        }
    }


    makeUnique( sol );

    while (P--) {
        int x = sol.back();
        sol.pop_back();
        printf("%d\n", x);
    }

    return 0;
}