Cod sursa(job #1710264)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 28 mai 2016 18:40:15
Problema Politie Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.61 kb
#include <bits/stdc++.h>

using namespace std;

constexpr int MAX_N = 250000;
constexpr int MAX_M = 300000;

struct Edge {
  int u, v;
  long long cost;

  inline bool operator <(const Edge &other) const {
    return cost < other.cost;
  }
} G[MAX_M];

int root[MAX_N];
int weight[MAX_N];
int costs[MAX_N - 1];

int getRoot(const int u) {
  if (u != root[u])
    return root[u] = getRoot(root[u]);
  return u;
}

void unionSet(int u, int v) {
  if (weight[u] < weight[v])
    swap(u, v);
  root[v] = u;
  weight[u] += (weight[u] == weight[v]);
}

int main() {
  ifstream fin("politie.in");
  ofstream fout("politie.out");
  fin.tie(0);
  ios_base::sync_with_stdio(0);

  int n, m, numQuality, maxDegree; fin >> n >> m >> numQuality >> maxDegree;

  for (int i = 0; i < m; i += 1) {
    int u, v, quality, degree; fin >> u >> v >> quality >> degree;
    G[i].u = u - 1;
    G[i].v = v - 1;
    G[i].cost = (1LL * quality << 32LL) | degree;
  }
  fin.close();

  sort(G, G + m);

  for (int i = 0; i < n; i += 1) {
    root[i] = i;
    weight[i] = 1;
  }

  int ptr = 0;
  for (int i = 0; i < m; i += 1) {
    const int u = getRoot(G[i].u);
    const int v = getRoot(G[i].v);
    if (u != v) {
      costs[ptr++] = (G[i].cost & ((1LL << 32) - 1));
      unionSet(u, v);
    }
  }

  sort(costs, costs + n - 1, greater <int>());

  int numUnique = 1;
  for (int i = 1; i < n - 1; i += 1)
    if (costs[i] != costs[numUnique - 1])
      costs[numUnique++] = costs[i];

  for (int i = 0; i < maxDegree; i += 1)
    fout << costs[i] << '\n';
  fout.close();

  return 0;
}