Cod sursa(job #1709087)

Utilizator lookingForAChallengeUBB Cociorva Popoveniuc Salajan lookingForAChallenge Data 28 mai 2016 10:50:59
Problema Politie Scor 100
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.4 kb
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second

const int nmax = 250005;
const int mmax = 500005;

int n, m, d, p;
pair< int, pair<int, int> > edge[mmax];
vector<int> v[nmax];
set<int> mySet;
int t[nmax];

bool cmp( int x, int y ){
	return edge[x].second.second < edge[y].second.second;
}

int findRad(int x){
	int x2 = x;
	while(t[x]){
		x = t[x];
	}
	while(x2 != x){
		int temp = t[x2];
		t[x2] = x;
		x2 = temp;
	}
	return x;
}

void uneste(int x, int y){
	if (x & 1){
		t[x] = y;
	}else{
		t[y] =x;
	}
}

int main() {
    cin.sync_with_stdio(false);

	freopen("politie.in", "r", stdin);
	freopen("politie.out", "w", stdout);

	cin >> n >> m >> d >> p;
	for(int i=1; i<=m; ++i){
		int x, y, categ, grad;
		cin >> x >> y >> categ >> grad;
		edge[i] = mp( x, mp(y, grad) );
		v[categ].pb( i );
	}
	for(int i=1; i<=d; ++i){
		sort(v[i].begin(), v[i].end(), cmp);
		for(int j=0; j<v[i].size(); ++j){
			int idx = v[i][j];
			int x = findRad(edge[idx].first);
			int y = findRad(edge[idx].second.first);
			int cost = edge[idx].second.second;
			if (x == y){
				continue;
			}
			uneste(x, y);
			mySet.insert(cost);
		}
	}

	set<int> ::iterator it = mySet.end();
	--it;
	for(int i=1; i<=p; ++i){
		cout << *it << "\n";
		--it;
	}



    return 0;
}