Cod sursa(job #2122747)

Utilizator Victor_IonescuVictor Ionescu Victor_Ionescu Data 5 februarie 2018 14:19:26
Problema Politie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.73 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>

#define MAXN 250005
#define MAXM 500005

using namespace std;

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

vector <int> sol;

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;
    }
};

int N, M, d, p, tata[MAXN];
str muchie[MAXM];

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(muchie[i].x);
        v2 = father(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;
}