Cod sursa(job #1714046)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 7 iunie 2016 11:17:06
Problema Politie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.41 kb
#include <bits/stdc++.h>

using namespace std;

struct Edge
{
    int x, y, t, c;

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

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

Edge edges[MAX_M + 1];
int father[MAX_N + 1];

int N, M, D, P;

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

void unite(int x, int y)
{
    father[ findRoot(x) ] = findRoot(y);
}

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

    in >> N >> M >> D >> P;

    for (int i = 1; i <= M; ++i)
        in >> edges[i].x >> edges[i].y >> edges[i].t >> edges[i].c;

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

    sort(edges + 1, edges + M + 1);

    vector<int> solution;

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

        if (findRoot(a) != findRoot(b))
        {
            unite(a, b);
            solution.push_back(edges[i].c);
        }
    }

    sort(solution.begin(), solution.end(), [](const int x, const int y){return x > y;});

    vector<int> tmp;

    for (size_t i = 0; i < solution.size(); ++i)
        if ((i == 0 || solution[i] != solution[i - 1]) && P > 0)
            tmp.push_back(solution[i]);

    tmp.resize(P);

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

    return 0;
}