結果

問題 No.1950 片道きゃっちぼーる
ユーザー emthrm
提出日時 2022-05-20 22:08:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 499 ms / 3,000 ms
コード長 3,755 bytes
コンパイル時間 2,839 ms
コンパイル使用メモリ 220,260 KB
最終ジャッジ日時 2025-01-29 10:44:07
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct StronglyConnectedComponents {
  std::vector<int> id;
  std::vector<std::vector<int>> vertices, g;

  StronglyConnectedComponents(const std::vector<std::vector<int>>& graph,
                              const bool is_full_ver = false)
      : is_full_ver(is_full_ver), n(graph.size()), is_used(n, false),
        graph(graph), rgraph(n) {
    order.reserve(n);
    for (int i = 0; i < n; ++i) {
      if (!is_used[i]) dfs(i);
    }
    id.assign(n, -1);
    for (int i = 0; i < n; ++i) {
      for (const int e : graph[i]) rgraph[e].emplace_back(i);
    }
    int m = 0;
    for (int i = n - 1; i >= 0; --i) {
      if (id[order[i]] == -1) {
        if (is_full_ver) vertices.emplace_back();
        rdfs(order[i], m++);
      }
    }
    g.resize(m);
    for (int i = 0; i < n; ++i) {
      for (const int e : graph[i]) {
        if (id[i] != id[e]) g[id[i]].emplace_back(id[e]);
      }
    }
    // if (is_full_ver) {
    //   for (int i = 0; i < m; ++i) {
    //     std::sort(vertices[i].begin(), vertices[i].end());
    //   }
    // }
  }

 private:
  const bool is_full_ver;
  const int n;
  std::vector<bool> is_used;
  std::vector<int> order;
  std::vector<std::vector<int>> graph, rgraph;

  void dfs(const int ver) {
    is_used[ver] = true;
    for (const int e : graph[ver]) {
      if (!is_used[e]) dfs(e);
    }
    order.emplace_back(ver);
  }

  void rdfs(const int ver, const int m) {
    id[ver] = m;
    if (is_full_ver) vertices.back().emplace_back(ver);
    for (const int e : rgraph[ver]) {
      if (id[e] == -1) rdfs(e, m);
    }
  }
};

std::vector<int> topological_sort(const std::vector<std::vector<int>>& graph) {
  const int n = graph.size();
  std::vector<int> deg(n, 0);
  for (int i = 0; i < n; ++i) {
    for (const int e : graph[i]) ++deg[e];
  }
  std::queue<int> que;
  for (int i = 0; i < n; ++i) {
    if (deg[i] == 0) que.emplace(i);
  }
  std::vector<int> res;
  res.reserve(n);
  while (!que.empty()) {
    const int ver = que.front();
    que.pop();
    res.emplace_back(ver);
    for (const int e : graph[ver]) {
      if (--deg[e] == 0) que.emplace(e);
    }
  }
  return res.size() == n ? res : std::vector<int>{};
}

int main() {
  int n; cin >> n;
  vector<ll> x(n), a(n);
  REP(i, n) cin >> x[i];
  REP(i, n) cin >> a[i];
  map<ll, int> m;
  REP(i, n) m[x[i]] = i;
  vector<vector<int>> graph(n);
  REP(i, n) {
    for (const ll d : vector<ll>{-a[i], a[i]}) {
      if (const auto it = m.find(x[i] + d); it != m.end()) graph[i].emplace_back(it->second);
    }
  }
  StronglyConnectedComponents scc(graph);
  vector<int> order = topological_sort(scc.g);
  reverse(ALL(order));
  vector<int> dp(scc.g.size(), 0);
  REP(i, n) chmax(dp[scc.id[i]], x[i] + a[i]);
  for (int i : order) {
    for (int j : scc.g[i]) chmax(dp[i], dp[j]);
  }
  REP(i, n) cout << dp[scc.id[i]] - x[i] << '\n';
  return 0;
}
0