結果

問題 No.1024 Children in a Row
ユーザー 👑 emthrmemthrm
提出日時 2020-04-14 23:46:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,711 bytes
コンパイル時間 3,045 ms
コンパイル使用メモリ 217,608 KB
実行使用メモリ 71,184 KB
最終ジャッジ日時 2024-04-09 18:23:25
合計ジャッジ時間 14,432 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 460 ms
69,748 KB
testcase_29 AC 199 ms
71,184 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;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, 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() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int n, m; cin >> n >> m;
  vector<int> a(m), b(m), k(m); REP(i, m) cin >> a[i] >> b[i] >> k[i], --a[i], --b[i];
  vector<vector<vector<pair<int, int>>>> graph(n, vector<vector<pair<int, int>>>(1));
  vector<int> dst(m);
  REP(i, m) {
    dst[i] = graph[a[i]].size();
    graph[a[i]].emplace_back();
    graph[b[i]].back().emplace_back(a[i], dst[i]);
  }
  // REP(i, m) cout << dst[i] << " \n"[i + 1 == m];
  vector<vector<int>> l(n), r(n);
  REP(i, n) {
    l[i].resize(graph[i].size());
    r[i].resize(graph[i].size());
  }
  vector<int> children;
  function<void(int, int)> dfs = [&](int src1, int src2) {
    l[src1][src2] = children.size();
    children.emplace_back(src1);
    reverse(ALL(graph[src1][src2]));
    for (auto [dst1, dst2] : graph[src1][src2]) dfs(dst1, dst2);
    r[src1][src2] = children.size();
  };
  REP(i, n) dfs(i, 0);
  // REP(i, n + m) cout << children[i] << " \n"[i + 1 == n + m];
  // REP(i, n) REP(j, graph[i].size()) cout << i << ' ' << j << " : " << l[i][j] << ' ' << r[i][j] << '\n';
  vector<int> latest(n);
  REP(i, n) latest[i] = l[i][graph[i].size() - 1];
  sort(ALL(latest));
  // REP(i, n) cout << latest[i] << " \n"[i + 1 == n];
  function<bool(int, int, int&)> solve = [&](int left, int right, int &k) {
    // cout << left << ' ' << right << '\n';
    int cnt = upper_bound(ALL(latest), right) - lower_bound(ALL(latest), left);
    if (cnt < k) {
      k -= cnt;
      return false;
    }
    int idx = lower_bound(ALL(latest), left) - latest.begin();
    cout << children[latest[idx + k - 1]] + 1 << '\n';
    return true;
  };
  REP(i, m) {
    int prev = l[a[i]][dst[i] - 1];
    assert(solve(0, prev, k[i])
           || solve(l[a[i]][dst[i]], r[a[i]][dst[i]] - 1, k[i])
           || solve(prev + 1, l[a[i]][dst[i]] - 1, k[i])
           || solve(r[a[i]][dst[i]], n + m - 1, k[i]));
  }
  return 0;
}
0