Cod sursa(job #1709416)

Utilizator TeamFIIBUAIC NoReturnValue TeamFIIB Data 28 mai 2016 12:09:23
Problema Politie Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.55 kb
#include <iostream>
#include<vector>
#include<fstream>
#include<utility>
#include<algorithm>
#include <deque>
#include <queue>

using namespace std;

int N, M, D, P;

struct Edge {
	int from, to, type, danger;
};

vector <Edge> edges;
vector <int> weight, root;
priority_queue <int> heap;

bool operator<(Edge a, Edge b) {
	 return make_pair(a.type, a.danger) < make_pair(b.type, b.danger);
}

int GetRoot(int x) {
	if (x == root[x]) {
	   return x;
	}
	
	return root[x] = GetRoot(root[x]);
}

bool Unite(int x, int y) {
	 x = GetRoot(x);
	 y = GetRoot(y);
	 if (x == y) {
	 	return false;
	 }
	 
	 if (weight[x] < weight[y]) {
		swap(x, y);
	 }
	 root[y] = x;
	 weight[x] += weight[y];
	 return true;
}

int main() {

	//ifstream cin("politie.in");
	//ofstream cout("politie.out");
	
	freopen ("politie.in", "r", stdin);
	freopen ("politie.out", "w", stdout);

	int i;

	scanf("%d %d %d %d", &N, &M, &D, &P);
	
	edges.resize(M);
	root.resize(1 + N);
	weight.resize(1 + N, 1);
	
	for (i = 1; i <= N; ++i) {
		root[i] = i;
	}
	
	for (i = 0; i < M; ++i) {
		scanf("%d %d %d %d", &edges[i].from, &edges[i].to, &edges[i].type, &edges[i].danger);
	}
	
	sort (edges.begin(), edges.end());
	
	for (i = 0; i < M; ++i) {
		int from, to;
		from = edges[i].from;
		to = edges[i].to;
		
		if (Unite(from, to)) {
		   heap.push(edges[i].danger);
		}
	}
	int last = 0;
	while (P--) {
		while (heap.size() && heap.top() == last) {
			  heap.pop();
		}
		last = heap.top();
		printf("%d\n", last);
	}

	return 0;
}