Cod sursa(job #1713829)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 6 iunie 2016 18:10:12
Problema Politie Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.61 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

ifstream cin("politie.in");
ofstream cout("politie.out");

const int NMAX = 250007;

struct pol {
    int x;
    int y;
    int type;
    int cost;
};

vector < int > Ans;
pol a[NMAX];
int H[NMAX], T[NMAX];
int n, m, D, p;

bool cmp(pol a, pol b) {
    if(a.type != b.type) {
        return a.type < b.type;
    }
    return a.cost < b.cost;
}

int father(int x) {
    if(T[x] == x){
        return x;
    }
    return father(T[x]);
}

void unite(int x, int y) {
    if(H[x] > H[y]) {
        T[y] = x;
    }
    else{
        if(H[x] < H[y]) {
            T[x] = y;
        }
        else {
            T[y] = x;
            ++H[x];
        }
    }
}

int main() {
    cin >> n >> m >> D >> p;
    for(int i = 1; i <= m; ++i) {
        int X, Y, Z, TT;
        cin >> X >> Y >> Z >> TT;
        a[i].x = X; a[i].y = Y; a[i].type = Z; a[i].cost = TT;
    }
    for(int i = 1; i <= n; ++i) {
        T[i] = i;
        H[i] = 1;
    }
    sort(a + 1, a + m + 1, cmp);
    for(int i = 1; i <= m; ++i) {
        int Tx = father(a[i].x);
        int Ty = father(a[i].y);
        if(Tx != Ty) {
            Ans.push_back(a[i].cost);
            unite(Tx, Ty);
        }
    }
    sort(Ans.begin(), Ans.end());
    reverse(Ans.begin(), Ans.end());
    for(int i = 0; i < Ans.size(); ++i) {
        if(Ans[i] != Ans[i - 1] || i == 0) {
            cout << Ans[i] << "\n";
            --p;
            if(p == 0) {
                return 0;
            }
        }
    }
    return 0;
}