結果

問題 No.1950 片道きゃっちぼーる
ユーザー 👑 emthrmemthrm
提出日時 2022-05-20 22:08:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 510 ms / 3,000 ms
コード長 3,755 bytes
コンパイル時間 3,586 ms
コンパイル使用メモリ 229,112 KB
実行使用メモリ 79,144 KB
最終ジャッジ日時 2023-10-20 12:50:12
合計ジャッジ時間 10,746 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 270 ms
79,144 KB
testcase_04 AC 259 ms
66,472 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 252 ms
65,748 KB
testcase_07 AC 200 ms
42,448 KB
testcase_08 AC 215 ms
42,448 KB
testcase_09 AC 247 ms
59,412 KB
testcase_10 AC 248 ms
61,812 KB
testcase_11 AC 253 ms
61,952 KB
testcase_12 AC 238 ms
61,952 KB
testcase_13 AC 276 ms
62,248 KB
testcase_14 AC 259 ms
54,840 KB
testcase_15 AC 510 ms
62,992 KB
testcase_16 AC 254 ms
59,412 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 241 ms
54,328 KB
testcase_19 AC 509 ms
61,524 KB
testcase_20 AC 211 ms
42,448 KB
testcase_21 AC 255 ms
56,052 KB
testcase_22 AC 249 ms
54,132 KB
権限があれば一括ダウンロードができます

ソースコード

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