Submission #612637

#TimeUsernameProblemLanguageResultExecution timeMemory
612637evenvalueHighway Tolls (IOI18_highway)C++17
51 / 100
179 ms22840 KiB
#include "highway.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;

using int64 = long long;
using ld = long double;

constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;

int find_hidden_node(const vector<vector<pair<int, int>>> &g, const int s, const int par, const int64 all_a, const vector<int> &template_w, const int a, const int b) {
  vector<int> w = template_w;

  function<void(int, int)> dfs1 = [&](const int x, const int p) {
    for (const auto &[y, i] : g[x]) {
      if (y == p) continue;
      w[i] = 1;
      dfs1(y, x);
    }
  };

  dfs1(s, par);
  const int64 this_b = ask(w);
  const int depth = (this_b - all_a) / (b - a);

  if (depth == 0) {
    return s;
  }

  vector<pair<int, int>> candidates;

  function<void(int, int, int, int)> dfs2 = [&](const int x, const int p, const int i, const int d) {
    if (d == depth) {
      candidates.emplace_back(x, i);
      return;
    }
    for (const auto &[y, idx] : g[x]) {
      if (y == p) continue;
      dfs2(y, x, idx, d + 1);
    }
  };

  dfs2(s, par, -1, 0);

  int lo = 0, hi = candidates.size() - 1;
  while (lo < hi) {
    const int mid = (lo + hi) / 2;
    for (int i = 0; i < candidates.size(); i++) {
      w[candidates[i].second] = (i <= mid);
    }
    if (ask(w) < this_b) {
      lo = mid + 1;
    } else {
      hi = mid;
    }
  }

  return candidates[lo].first;
}

void find_pair(const int n, const vector<int> U, const vector<int> V, const int a, const int b) {
  const int m = U.size();

  vector<int> w(m);
  const int64 all_a = ask(w);

  int lo = 0, hi = m - 1;
  while (lo < hi) {
    const int mid = (lo + hi) / 2;
    for (int i = 0; i < m; i++) {
      w[i] = (i <= mid);
    }
    if (ask(w) > all_a) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }

  assert(lo == hi);

  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < m; i++) {
    g[U[i]].emplace_back(V[i], i);
    g[V[i]].emplace_back(U[i], i);
  }

  auto bfs = [&g](const int s) {
    vector<int> dist(g.size(), kInf);
    dist[s] = 0;
    queue<int> q;
    q.push(s);

    while (not q.empty()) {
      const int x = q.front();
      for (const auto &[y, _] : g[x]) {
        if (dist[y] < kInf) continue;
        q.push(y);
        dist[y] = dist[x] + 1;
      }
      q.pop();
    }

    return dist;
  };

  auto bfs_tree = [&](const int s, vector<int> near) -> vector<vector<pair<int, int>>> {
    queue<int> q;
    q.push(s);
    vector<vector<pair<int, int>>> tree(n);

    near[s] = 0;
    while (not q.empty()) {
      const int x = q.front();
      for (const auto &[y, i] : g[x]) {
        if (near[y] == 0) continue;
        q.push(y);
        tree[x].emplace_back(y, i);
        near[y] = 0;
      }
      q.pop();
    }

    return tree;
  };

  const int x = U[lo], y = V[lo];
  const vector<int> distx = bfs(x), disty = bfs(y);

  vector<int> nearx(n), neary(n);
  for (int i = 0; i < n; i++) {
    nearx[i] = (distx[i] < disty[i]);
    neary[i] = (disty[i] < distx[i]);
  }

  const auto tx = bfs_tree(x, nearx);
  const auto ty = bfs_tree(y, neary);

  vector<int> template_w(m, 1);
  template_w[lo] = 0;
  for (int u = 0; u < n; u++) {
    for (const auto &[_, i] : tx[u]) {
      template_w[i] = 0;
    }
    for (const auto &[_, i] : ty[u]) {
      template_w[i] = 0;
    }
  }

  assert(ask(template_w) == all_a);

  answer(find_hidden_node(tx, x, y, all_a, template_w, a, b),
         find_hidden_node(ty, y, x, all_a, template_w, a, b));
}

Compilation message (stderr)

highway.cpp: In function 'int find_hidden_node(const std::vector<std::vector<std::pair<int, int> > >&, int, int, int64, const std::vector<int>&, int, int)':
highway.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (int i = 0; i < candidates.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...