Cod sursa(job #1708574)

Utilizator neapuiuComisie ICPC UPB neapuiu Data 27 mai 2016 14:13:41
Problema Politie Scor Ascuns
Compilator cpp Status done
Runda Marime 2.32 kb
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   politie.cpp
 * Author: Stefan
 *
 * Created on May 27, 2016, 1:39 PM
 */

#include <cstdlib>
#include <vector>
#include <stdio.h>
#include <algorithm>

using namespace std;

#define NMAX 250001

int n, d, p, m;

struct edge {
    int x, y, t, c;
    bool usable;
};

bool cmp(const edge &a, const edge &b) {
    return a.t < b.t;
}

bool cmpDanger(const edge &a, const edge &b) {
    return a.c > b.c;
}

int parent[NMAX], depth[NMAX], cat[NMAX];


int getParent(int node) {
    if (parent[node] == node) return node;
    int p = getParent(parent[node]);
    parent[node] = p;
    return p;
}
/*
 * 
 */
int main(int argc, char** argv) {
    freopen ("politie.in","r",stdin);
    freopen ("politie.out","w",stdout);
    scanf("%d %d %d %d", &n, &m, &d, &p);
    vector<edge> edges;
    for (int i = 0; i < m; i++) {
        edge e;
        scanf("%d %d %d %d", &(e.x), &(e.y), &(e.t), &(e.c) );
        e.x --;
        e.y --;
        e.usable = true;
        edges.push_back(e);
    }
    sort(edges.begin(), edges.end(), cmp);
    for (int i = 0; i < n; i++) {
        parent[i] = i;
        depth[i] = 1;
        cat[i] = 1;
    }
    for (int i = 0; i < m; i++) {
        edge &e = edges[i];
        int p1 = getParent(e.x);
        int p2 = getParent(e.y);
        if (p1 == p2) {
            if (edges[i].t > cat[p1]) {
                e.usable = false;
            }
        }
        else {
            int maxCat = max(cat[p1], cat[p2]);
            if (depth[p1] >= depth[p2]) {
                parent[p2] = p1;
                if (depth[p1] == depth[p2]) depth[p1] ++;
                cat[p1] = max(maxCat, e.t);
            }
            else {
                parent[p1] = p2;
                cat[p2] = max(maxCat, e.t);
            }
        }
    }
    sort(edges.begin(), edges.end(), cmpDanger);
    int last = -1;
    for (int i = 0; i < m; i++) {
        edge &e = edges[i];
        if (e.usable && last != e.c) {
            printf("%d\n", e.c);
            last = e.c;
            p --;
            if (p == 0) break;
        }
    }
    return 0;
}