結果

問題 No.1024 Children in a Row
ユーザー 👑 emthrmemthrm
提出日時 2020-04-15 00:09:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 573 ms / 2,000 ms
コード長 3,022 bytes
コンパイル時間 2,812 ms
コンパイル使用メモリ 216,976 KB
実行使用メモリ 70,756 KB
最終ジャッジ日時 2024-04-09 18:24:14
合計ジャッジ時間 15,733 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 481 ms
42,608 KB
testcase_11 AC 496 ms
37,868 KB
testcase_12 AC 479 ms
39,800 KB
testcase_13 AC 493 ms
40,900 KB
testcase_14 AC 493 ms
39,944 KB
testcase_15 AC 496 ms
42,060 KB
testcase_16 AC 467 ms
39,092 KB
testcase_17 AC 470 ms
43,740 KB
testcase_18 AC 389 ms
39,776 KB
testcase_19 AC 561 ms
53,008 KB
testcase_20 AC 539 ms
52,872 KB
testcase_21 AC 555 ms
53,120 KB
testcase_22 AC 570 ms
53,008 KB
testcase_23 AC 555 ms
51,992 KB
testcase_24 AC 573 ms
52,116 KB
testcase_25 AC 536 ms
51,996 KB
testcase_26 AC 505 ms
54,360 KB
testcase_27 AC 445 ms
50,712 KB
testcase_28 AC 466 ms
69,620 KB
testcase_29 AC 203 ms
70,756 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) {
    if (l[a[i]][dst[i] - 1] < l[a[i]][dst[i]]) {
      assert(solve(0, l[a[i]][dst[i] - 1], k[i])
            || solve(l[a[i]][dst[i]], r[a[i]][dst[i]] - 1, k[i])
            || solve(l[a[i]][dst[i] - 1] + 1, l[a[i]][dst[i]] - 1, k[i])
            || solve(r[a[i]][dst[i]], n + m - 1, k[i]));
    } else {
      assert(solve(0, l[a[i]][dst[i]] - 1, k[i])
            || solve(r[a[i]][dst[i]], l[a[i]][dst[i] - 1], k[i])
            || solve(l[a[i]][dst[i]], r[a[i]][dst[i]] - 1, k[i])
            || solve(l[a[i]][dst[i] - 1] + 1, n + m - 1, k[i]));
    }
  }
  return 0;
}
0