Cod sursa(job #2122723)

Utilizator CammieCamelia Lazar Cammie Data 5 februarie 2018 13:57:28
Problema Politie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.74 kb
#include <fstream>
#include <algorithm>
#include <vector>

#define MAXN 250002
#define MAXM 500005

using namespace std;

ifstream fin("politie.in");
ofstream fout("politie.out");

struct str{
    int x, y, tip, c;

    bool operator < (const str& other) const {
        if (tip == other.tip)
            return c < other.c;
        return tip < other.tip;
    }
};

vector <int> sol;

str muchie[MAXM];
int n, m, d, p, tata[MAXN];

inline void Read() {
    fin >> n >> m >> d >> p;

    for (int i = 1; i <= m; i++) {
        fin >> muchie[i].x >> muchie[i].y >> muchie[i].tip >> muchie[i].c;
    }

    sort(muchie + 1, muchie + m + 1);
}

inline int father(int node) {
    if (node != tata[node]) {
        tata[node] = father(tata[node]);
    }
    return tata[node];
}

inline void APM() {
    int v1, v2, contor = 0;

    for (int i = 1; i <= n; i++)
        tata[i] = i;

    for (int i = 1; i <= m; i++) {
        v1 = father(tata[muchie[i].x]);
        v2 = father(tata[muchie[i].y]);

        if (v1 != v2) {
            tata[v1] = v2;
            contor++;
            sol.push_back(muchie[i].c);

            if (contor == n - 1) {
                return;
            }
        }
    }
}

bool descr(int A, int B) {
    return A > B;
}

inline void Afisare() {
    int l = sol.size(), i = 0, contor = 0;

    sort(sol.begin(), sol.end(), descr);

    sol.push_back(0); l++;

    while (i < l - 1) {
        fout << sol[i] << "\n";
        contor++;

        if (contor == p)
            return;

        while (sol[i] == sol[i + 1])
            i++;
        i++;
    }
}

int main () {
    Read();
    APM();
    Afisare();

    fin.close(); fout.close(); return 0;
}