Cod sursa(job #1709660)

Utilizator UNIBUC_Costan_Iordache_MagureanuGangster Teddy Bears Trio UNIBUC_Costan_Iordache_Magureanu Data 28 mai 2016 13:17:38
Problema Politie Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.63 kb
#include <fstream>
#include <algorithm>
#include <vector>

#define DIM 500010

using namespace std;

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

int n, m, d, p;
int parent[DIM];

int root(int x) {

    int node = x;

    while (parent[x] > 0)
        x = parent[x];

    while(parent[node] > 0) {

        int y = parent[node];
        parent[node] = x;
        node = y;

    }

    return x;
}

struct data {

    int x;
    int y;
    int t;
    int c;

}v[DIM];

bool cmp(const data &a, const data &b) {

    if(a.t != b.t)
        return a.t < b.t;
    return a.c < b.c;

}

bool cmp1(const int &a, const int &b) {

    return a > b;

}

int l[DIM];

int main() {

    fin >> n >> m >> d >> p;

    for(int i = 1; i <= m; i++)
        fin >> v[i].x >> v[i].y >> v[i].t >> v[i].c;

    sort(v + 1, v + m + 1, cmp);

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

    int k = 0;

    for(int i = 1; i <= m; i++) {

        int rootx = root(v[i].x);
        int rooty = root(v[i].y);

        if(rootx == rooty)
            continue;

        if (parent[rootx] < parent[rooty]) {
            parent[rootx] += parent[rooty];
            parent[rooty] = rootx;
        }else {
            parent[rooty] += parent[rootx];
            parent[rootx] = rooty;
        }

        l[++k] = v[i].c;

    }

    sort(l + 1, l + k + 1, cmp1);

    k = 1;
    fout << l[1] << "\n";

    for(int i = 2; k < p; i++) {

         if (l[i] != l[i - 1]) {

            fout << l[i] << "\n";
            k++;

         }

    }

    return 0;

}