Cod sursa(job #1709335)

Utilizator team_nameUPB Banu Popa Visan team_name Data 28 mai 2016 11:51:26
Problema Politie Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.61 kb
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;

const int nmax = 250010;

int n, m, d, p, x, y, t, c;
vector<pair<pair<int, int>, pair<int, int> > > e;
int father[nmax], size[nmax];
vector<int> cst;

int root(int x)
{
    int p, y;
    for (p = x; p != father[p]; p = father[p]);
    for (; x != p; )
    {
        y = father[x];
        father[x] = p;
        x = y;
    }
    return p;
}

void unite(int rx, int ry)
{
    if (size[rx] >= size[ry]) father[ry] = rx, size[rx] += size[ry];
    else father[rx] = ry, size[ry] += size[rx];
}

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

    scanf("%i %i %i %i", &n, &m, &d, &p);
    for (int i = 1; i <= m; ++ i)
    {
        scanf("%i %i %i %i", &x, &y, &t, &c);
        e.push_back(make_pair(make_pair(t, c), make_pair(x, y)));
    }

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

    for (int i = 1; i <= n; ++ i)
        father[i] = i, size[i] = 1;

    for (int i = 0; i < e.size(); ++ i)
    {
        int cost = e[i].first.second, nodea = e[i].second.first, nodeb = e[i].second.second;
        int ra = root(nodea), rb = root(nodeb);
        if (ra != rb)
        {
            cst.push_back(cost);
            unite(ra, rb);
        }
    }

    sort(cst.begin(), cst.end());
    reverse(cst.begin(), cst.end());

    int cntdist = 1;
    printf("%d\n", cst[0]);
    if (p == 1)
        return 0;
    for (int i = 1; i < cst.size(); ++ i)
        if (cst[i] != cst[i - 1])
        {
            cntdist ++;
            printf("%d\n", cst[i]);
            if (cntdist == p)
                break;
        }
}