Cod sursa(job #1709825)

Utilizator Spiromanii_MessiUNIBUCThrowTerror Spiromanii_Messi Data 28 mai 2016 14:03:07
Problema Politie Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.94 kb
#include <cstdio>
#include <algorithm>

using namespace std;

const int M = 500010, N = 250010;

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

class MyComp {
public :
    bool operator () (const Edge &A, const Edge &B) {
        return A.t < B.t || (A.t == B.t && A.c < B.c);
    }
};

class MyComp2 {
public :
    bool operator () (const int &A, const int &B) {
        return A > B;
    }
};

Edge E [M];
int ans [M], t [N], h [N];

int getFather (int x) {
    int T, father;

    for (T = t [x]; t [T] != T; T = t [T]);
    father = T;
    while (x != father) {
        T = t [x];
        t [x] = father;
        x = T;
    }
    return father;
}

void unite (int x, int y) {
    if (h [x] >= h [y]) {
        t [y] = x;
        if (h [x] == h [y])
            h [x] ++;
    }
    else {
        t [x] = y;
    }
}

int main () {
    int n, m, d, p, num = 0, i, fx, fy, x, y, tt, c;

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

    scanf ("%d%d%d%d", &n, &m, &d, &p);
    for (i = 1; i <= n; i ++) {
        t [i] = i;
        h [i] = 0;
    }
    for (i = 1; i <= m; i ++) {
        scanf ("%d%d%d%d", &x, &y, &tt, &c);
        E [i].x = x;
        E [i].y = y;
        E [i].t = tt;
        E [i].c = c;
    }

    sort (E + 1, E + 1 + m, MyComp ());

    for (i = 1; i <= m && num < n - 1; i ++) {
        fx = getFather (E [i].x);
        fy = getFather (E [i].y);
        if (fx != fy) {
            unite (fx, fy);
            num ++;
            ans [++ ans [0]] = E [i].c;
        }
    }
    sort (ans + 1, ans + 1 + ans [0], MyComp2 ());
    num = 0;
    ans [ans [0] + 1] = ans [ans [0]] - 20;
    for (i = 1; i <= ans [0]; i ++) {
        if (ans [i] == ans [i + 1])
            continue;
        else {
            printf ("%d\n", ans [i]);
            num ++;
            if (num == p)
               break;
        }
    }
    return 0;
}