Submission #612541

#TimeUsernameProblemLanguageResultExecution timeMemory
612541evenvalueHighway Tolls (IOI18_highway)C++17
0 / 100
131 ms35460 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;

vector<int> invert(const vector<int> &partition) {
  vector<int> new_partition = partition;
  for (int &x : new_partition) {
    x = 1 - x;
  }
  return new_partition;
}

int find_st(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) {
  const int m = (int)count_if(g.begin(), g.end(), [](const vector<pair<int, int>> &node) {
                  return not node.empty();
                })- 1;

  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 int 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 = lo; i <= mid; i++) {
      w[candidates[i].second] = 1;
    }
    for (int i = mid + 1; i <= hi; i++) {
      w[candidates[i].second] = 0;
    }
    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 = -1, hi = m - 1;
  while (lo + 1 < hi) {
    const int mid = (lo + hi) / 2;
    for (int i = lo + 1; i <= mid; i++) {
      w[i] = 1;
    }
    if (const int64 c = ask(w); c > all_a) {
      hi = mid;
      for (int i = lo + 1; i <= mid; i++) {
        w[i] = 0;
      }
    } else {
      lo = mid + 1;
    }
  }

  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 = [&](const int s) {
    vector<int> dist(n, kInf);
    dist[s] = 0;
    queue<int> q;
    q.push(s);
    vector<bool> visit(n, false);
    visit[s] = true;

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

    return dist;
  };

  vector<int> template_w(m, 1);

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

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

    return tree;
  };

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

  vector<int> partition(n);
  for (int i = 0; i < n; i++) {
    partition[i] = (distx[i] < disty[i] ? 0 : 1);
  }

  const auto tx = bfs_tree(x, partition), ty = bfs_tree(y, invert(partition));
  answer(find_st(tx, x, y, all_a, template_w, a, b),
         find_st(ty, y, x, all_a, template_w, a, b));
}

Compilation message (stderr)

highway.cpp: In function 'int find_st(const std::vector<std::vector<std::pair<int, int> > >&, int, int, int64, const std::vector<int>&, int, int)':
highway.cpp:34:13: warning: unused variable 'm' [-Wunused-variable]
   34 |   const int m = (int)count_if(g.begin(), g.end(), [](const vector<pair<int, int>> &node) {
      |             ^
#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...