QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#253701#7791. 通道建设 Passage Constructionhos_lyric#Compile Error//C++143.5kb2023-11-17 12:04:552024-07-04 02:26:06

Judging History

你现在查看的是最新测评结果

  • [2024-07-04 02:26:06]
  • 评测
  • [2023-11-17 12:04:55]
  • 提交

answer

#include "passageconstruction.h"

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


mt19937_64 rng(59);

int N, Subtask;
vector<vector<int>> graph;
vector<pair<int, int>> ans;

int dfs(int u) {
  int ret = -1;
  for (const int v : graph[u]) {
    const int res = dfs(v);
    if (~res) {
      if (~ret) {
        ans.emplace_back(ret, res);
        ret = -1;
      } else {
        ret = res;
      }
    }
  }
  if (~ret) {
    ans.emplace_back(ret, u);
    return -1;
  } else {
    return u;
  }
}

vector<pair<int, int>> ConstructPassages(int N_, const vector<pair<int, int>> &E) {
  N = N_;
  Subtask = getSubtaskID();
  const int rt = (Subtask == 4 || Subtask == 6) ? (N + 1 + rng() % N) : (1 + rng() % (2 * N));
  vector<int> dep(2 * N + 1, -1), dep1(2 * N + 1, -1);
  int rt1 = rt;
  for (int u = 1; u <= 2 * N; ++u) {
    dep[u] = GetDistance(rt, u);
    if (dep[rt1] < dep[u]) {
      rt1 = u;
    }
  }
  for (int u = 1; u <= 2 * N; ++u) {
    dep1[u] = GetDistance(rt1, u);
  }
  vector<int> par(2 * N + 1, -1);
  if (Subtask == 4 || Subtask == 6) {
    for (const auto &e : E) {
      int u = e.first, v = e.second;
      if (dep[u] > dep[v]) {
        swap(u, v);
      }
      assert(dep[u] + 1 == dep[v]);
      par[v] = u;
    }
  }
  vector<int> path(dep[rt1] + 1, -1);
  for (int u = 1; u <= 2 * N; ++u) if (dep[rt1] == dep[u] + dep1[u]) {
    path[dep[u]] = u;
  }
  for (int d = 1; d <= dep[rt1]; ++d) {
    par[path[d]] = path[d - 1];
  }
  vector<vector<int>> layers(2 * N);
  for (int u = 1; u <= 2 * N; ++u) {
    layers[dep[u]].push_back(u);
  }
  for (int d = 1; d < 2 * N; ++d) {
    for (const int v : layers[d]) if (!~par[v]) {
      vector<int> us;
      for (int u = 1; u <= 2 * N; ++u) if (dep[u] + 1 == dep[v] && dep1[u] + 1 == dep1[v]) {
        us.push_back(u);
      }
      const int usLen = us.size();
      assert(usLen);
      shuffle(us.begin(), us.end(), rng);
      for (int j = 0; j < usLen; ++j) {
        const int u = us[j];
        if (j == usLen - 1 || QueryLCA({rt}, {u, v}, u)[0]) {
          par[v] = u;
          break;
        }
      }
    }
  }
  graph.assign(2 * N + 1, {});
  for (int u = 1; u <= 2 * N; ++u) if (~par[u]) {
    graph[par[u]].push_back(u);
  }
  dfs(rt);
  return ans;
}

Details