Pagini recente » Cod sursa (job #996177) | Cod sursa (job #850364) | Cod sursa (job #3289228) | Cod sursa (job #2770015) | Cod sursa (job #2455988)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("politie.in");
ofstream g("politie.out");
int N, M, D, P, R[500005], TT[500005];
struct Edge{
int x, y, t, c;
} E[500005];
int Res[500005], cnt;
bool cmp(Edge a, Edge b){
return make_pair(a.t, a.c) < make_pair(b.t, b.c);
}
void Read(){
f >> N >> M >> D >> P;
for(int i = 1; i <= M; i++){
f >> E[i].x >> E[i].y >> E[i].t >> E[i].c;
}
for(int i = 1; i <= N; i++)
TT[i] = i;
sort(E + 1, E + M + 1, cmp);
}
void Unite(int x, int y){
if(x == y)
return;
if(R[x] < R[y]){
TT[x] = y;
}
else
TT[y] = x;
if(R[x] == R[y])
++R[x];
}
int Father(int x){
int init = x;
while(x != TT[x])
x = TT[x];
while(init != x){
int next = TT[init];
TT[init] = x;
init = next;
}
return x;
}
void Solve(){
for(int i = 1; i <= M; i++){
int x = E[i].x, y = E[i].y;
if(Father(x) != Father(y)){
Res[++cnt] = E[i].c;
Unite(Father(x), Father(y));
}
}
sort(Res + 1, Res + cnt + 1);
for(int i = cnt; i >= 1 && P > 0; i--){
if(i == cnt || Res[i] != Res[i + 1]){
g << Res[i] << "\n";
P--;
}
}
}
int main()
{
Read();
Solve();
return 0;
}