Cod sursa(job #1792135)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 30 octombrie 2016 01:35:23
Problema Politie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.44 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

constexpr int MAX_N = 250000 + 1;
constexpr int MAX_M = 500000 + 1;

struct Edge{
    int x, y;
    int cat, cost;

    bool operator < (const Edge &edge) const{
        if (cat != edge.cat)
            return cat < edge.cat;
        else
            return cost < edge.cost;
    }
};

int root[MAX_N];
Edge edges[MAX_M];

int father(int x){
    return x == root[x] ? x : root[x] = father(root[x]);
}

void unite(int x, int y){
    if (rand() & 1)
        swap(x, y);

    x = father(x);
    y = father(y);

    root[x] = y;
}

int main()
{
    ifstream in("politie.in");
    ofstream out("politie.out");

    int N, M, D, P;
    in >> N >> M >> D >> P;

    for (int i = 1; i <= N; ++i)
        root[i] = i;

    srand(time(nullptr));

    for (int i = 1; i <= M; ++i){
        in >> edges[i].x >> edges[i].y;
        in >> edges[i].cat >> edges[i].cost;
    }

    sort(edges + 1, edges + M + 1);
    vector<int> costs;

    for (int i = 1; i <= M; ++i){
        int x = edges[i].x;
        int y = edges[i].y;

        if (father(x) != father(y)){
            unite(x, y);
            costs.push_back(edges[i].cost);
        }
    }

    sort(costs.begin(), costs.end());
    costs.erase(unique(costs.begin(), costs.end()), costs.end());
    reverse(costs.begin(), costs.end());
    costs.resize(P);

    for (int x : costs)
        out << x << "\n";

    return 0;
}