记录编号 601507 评测结果 AAAATTTTTT
题目名称 4164.[CF1889D] Game of Stack 最终得分 40
用户昵称 GravatarLikableP 是否通过 未通过
代码语言 C++ 运行时间 12.128 s
提交时间 2025-06-25 16:28:00 内存使用 10.08 MiB
显示代码纯文本
#include <cstdio>
#include <vector>
#define isdigit(ch) ((ch) >= '0' && (ch) <= '9')

namespace IO {
template <typename T>
T read() {
  T res = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    res = (res << 3) + (res << 1) + (ch ^ 48);
  return res * f;
}

template <typename T>
void write(T x, char ed = '\n') {
  if (x < 0)
    x = -x, putchar('-');
  static int sta[sizeof(T) << 2], top = 0;
  do {
    sta[++top] = x % 10;
    x /= 10;
  } while (x);
  while (top) {
    putchar(sta[top--] ^ 48);
  }
  putchar(ed);
}
} // namespace IO
using namespace IO;

const int MAXN = 1e5 + 10;

int n;
int k[MAXN], cnt[MAXN];
::std::vector<int> sta[MAXN];

int main() {
  freopen("stack.in", "r", stdin);
  freopen("stack.out", "w", stdout);
  n = read<int>();
  for (int i = 1; i <= n; ++i) {
    k[i] = read<int>();
    sta[i].resize(k[i] + 1, 0);
    for (int j = 1; j <= k[i]; ++j) {
      sta[i][j] = read<int>();
    }
  }

  for (int st = 1; st <= n; ++st) {
    for (int i = 1; i <= n; ++i) {
      cnt[i] = 0;
    }
    int now = st;
    cnt[now]++;
    while (cnt[now] != k[now] + 1) {
      now = sta[now][k[now] - cnt[now] + 1];
      cnt[now]++;
    }
    write(now, ' ');
  }
  return 0;
}