Cod sursa(job #1709615)

Utilizator oldscotchUPB Old Scotch oldscotch Data 28 mai 2016 13:06:03
Problema Politie Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 2.04 kb
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cassert>

using namespace std;

struct edge
{
    int from, to, type, danger;
    bool operator < (const edge& other) const
    {
        return type < other.type || (type == other.type && danger < other.danger);
    }
};

const int NMAX = 250505;

const int LMAX = 505005;
int N, M, D, P, par[NMAX], deg[NMAX];
vector<int> candidates;
edge H[LMAX];

void read()
{
    scanf("%d%d%d%d", &N, &M, &D, &P);
    for (int i = 1; i <= M; i++)
    {
        scanf("%d%d%d%d", &H[i].from, &H[i].to, &H[i].type, &H[i].danger);
    }
}

void makeSet(int node)
{
    par[node] = node;
    deg[node] = 0;
}

int findRoot(int node)
{
    int x = node, aux;
    while (par[x] != x)
    {
        x = par[x];
    }

    while (par[node] != node)
    {
        aux = par[node];
        par[node] = x;
        node = aux;
    }

    return x;
}

void unite(int x, int y)
{
    if (deg[x] < deg[y])
    {
        par[x] = y;
    }
    else
    {
        par[y] = x;
        if (deg[x] == deg[y])
        {
            deg[x]++;
        }
    }
}

void prepare()
{
    for (int i = 1; i <= N; i++)
    {
        makeSet(i);
    }

    sort(H + 1, H + M + 1);
}

void solve()
{
    for (int i = 1; i <= M; i++)
    {
        if (findRoot(H[i].from) != findRoot(H[i].to))
        {
            candidates.push_back(H[i].danger);
            unite(findRoot(H[i].from), findRoot(H[i].to));
        }
    }
    sort(candidates.begin(), candidates.end());
    candidates.resize(unique(candidates.begin(), candidates.end()) - candidates.begin());
    reverse(candidates.begin(), candidates.end());
    // Should not happen. If it does...something is wrong
    assert (candidates.size() >= P);
    for (size_t i = 0; i < P; i++)
    {
        printf("%d\n", candidates[i]);
    }
}

int main()
{
    freopen("politie.in", "r", stdin);
    freopen("politie.out", "w", stdout);
    read();
    prepare();
    solve();
    return 0;

}