CF1889D.Game of Stacks

NOI/NOI+/CTSC

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have nn stacks r1,r2,…,rnr_1,r_2,\ldots,r_n. Each stack contains some positive integers ranging from 11 to nn.

Define the following functions:

function init(pos):    stacks := an array that contains n stacks r[1], r[2], ..., r[n]    return get(stacks, pos)function get(stacks, pos):    if stacks[pos] is empty:        return pos    else:        new_pos := the top element of stacks[pos]        pop the top element of stacks[pos]        return get(stacks, new_pos)

You want to know the values returned by init(1),init(2),…,init(n)\texttt{init(1)}, \texttt{init(2)}, \ldots, \texttt{init(n)}.

Note that, during these calls, the stacks r1,r2,…,rnr_1,r_2,\ldots,r_n don't change, so the calls init(1),init(2),…,init(n)\texttt{init(1)}, \texttt{init(2)}, \ldots, \texttt{init(n)} are independent.

你有 nn 个栈 r1,r2,…,rnr_1, r_2, \ldots, r_n。每个栈中包含若干个取值在 11 到 nn 之间的正整数。

定义如下函数:

function init(pos):    stacks := 一个包含 $n$ 个栈 $r[1], r[2], \ldots, r[n]$ 的数组    return get(stacks, pos)function get(stacks, pos):    if stacks[pos] 为空:        return pos    else:        new_pos := stacks[pos] 的栈顶元素        将 stacks[pos] 的栈顶元素弹出        return get(stacks, new_pos)

你需要求出 init(1),init(2),…,init(n)\texttt{init(1)}, \texttt{init(2)}, \ldots, \texttt{init(n)} 各自返回的值。

注意:在这些调用过程中,栈 r1,r2,…,rnr_1, r_2, \ldots, r_n 均不发生改变,因此调用 init(1),init(2),…,init(n)\texttt{init(1)}, \texttt{init(2)}, \ldots, \texttt{init(n)} 是相互独立的。

输入格式

The first line of the input contains one integer nn (1≤n≤1051\le n\le 10^5) — the length of the array rr.

Each of the following nn lines contains several integers. The first integer kik_i (0≤ki≤1050\le k_i\le 10^5) represents the number of elements in the ii-th stack, and the following kik_i positive integers ci,1,ci,2,…,ci,kic_{i,1},c_{i,2},\ldots,c_{i,k_i} (1≤ci,j≤n1\le c_{i,j}\le n) represent the elements in the ii-th stack. ci,1c_{i,1} is the bottom element.

In each test, ∑ki≤106\sum k_i\le 10^6.

输入的第一行包含一个整数 nn(1≤n≤1051\le n\le 10^5)—— 表示数组 rr 的长度。

接下来的 nn 行,每行包含若干个整数。第一个整数 kik_i(0≤ki≤1050\le k_i\le 10^5)表示第 ii 个栈中元素的个数;随后的 kik_i 个正整数 ci,1,ci,2,…,ci,kic_{i,1},c_{i,2},\ldots,c_{i,k_i}(1≤ci,j≤n1\le c_{i,j}\le n)表示第 ii 个栈中的元素,其中 ci,1c_{i,1} 为栈底元素。

在每个测试用例中,满足 ∑ki≤106\sum k_i\le 10^6。

输出格式

You need to output nn values, the ii-th of which is the value returned by init(i)\texttt{init(i)}.

你需要输出 nn 个值,其中第 ii 个值为 init(i)\texttt{init(i)} 返回的值。

输入输出样例

  • 输入#1

    3
    3 1 2 2
    3 3 1 2
    3 1 2 1

    输出#1

    1 2 2
  • 输入#2

    5
    5 1 2 4 3 4
    6 1 2 5 3 3 4
    6 1 1 4 4 4 2
    9 3 1 4 2 3 5 5 1 2
    4 4 4 1 3

    输出#2

    1 1 1 1 1

说明/提示

In the first example:

  • When you call init(1)\texttt{init(1)}, set stacks := [[1,2,2],[3,1,2],[1,2,1]]\texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]}, and then call get(stacks, 1)\texttt{get(stacks, 1)}.
    • stacks[1]\texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1]\texttt{stacks[1]}, which makes stacks\texttt{stacks} become [[1,2],[3,1,2],[1,2,1]][[1,2],[3,1,2],[1,2,1]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1,2],[3,1],[1,2,1]][[1,2],[3,1],[1,2,1]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1,2],[3],[1,2,1]][[1,2],[3],[1,2,1]], and then call get(stacks, 1)\texttt{get(stacks, 1)}.
    • stacks[1]\texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1]\texttt{stacks[1]}, which makes stacks\texttt{stacks} become [[1],[3],[1,2,1]][[1],[3],[1,2,1]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 3}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1],[],[1,2,1]][[1],[],[1,2,1]], and then call get(stacks, 3)\texttt{get(stacks, 3)}.
    • stacks[3]\texttt{stacks[3]} is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[3]\texttt{stacks[3]}, which makes stacks\texttt{stacks} become [[1],[],[1,2]][[1],[],[1,2]], and then call get(stacks, 1)\texttt{get(stacks, 1)}.
    • stacks[1]\texttt{stacks[1]} is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[1]\texttt{stacks[1]}, which makes stacks\texttt{stacks} become [[],[],[1,2]][[],[],[1,2]], and then call get(stacks, 1)\texttt{get(stacks, 1)}.
    • stacks[1]\texttt{stacks[1]} is empty, return 11.
  • When you call init(2)\texttt{init(2)}, set stacks := [[1,2,2],[3,1,2],[1,2,1]]\texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]}, and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1,2,2],[3,1],[1,2,1]][[1,2,2],[3,1],[1,2,1]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1,2,2],[3],[1,2,1]][[1,2,2],[3],[1,2,1]], and then call get(stacks, 1)\texttt{get(stacks, 1)}.
    • stacks[1]\texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1]\texttt{stacks[1]}, which makes stacks\texttt{stacks} become [[1,2],[3],[1,2,1]][[1,2],[3],[1,2,1]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 3}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1,2],[],[1,2,1]][[1,2],[],[1,2,1]], and then call get(stacks, 3)\texttt{get(stacks, 3)}.
    • stacks[3]\texttt{stacks[3]} is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[3]\texttt{stacks[3]}, which makes stacks\texttt{stacks} become [[1,2],[],[1,2]][[1,2],[],[1,2]], and then call get(stacks, 1)\texttt{get(stacks, 1)}.
    • stacks[1]\texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1]\texttt{stacks[1]}, which makes stacks\texttt{stacks} become [[1],[],[1,2]][[1],[],[1,2]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is empty, return 22.
  • When you call init(3)\texttt{init(3)}, set stacks := [[1,2,2],[3,1,2],[1,2,1]]\texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]}, and then call get(stacks, 3)\texttt{get(stacks, 3)}.
    • stacks[3]\texttt{stacks[3]} is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[3]\texttt{stacks[3]}, which makes stacks\texttt{stacks} become [[1,2,2],[3,1,2],[1,2]][[1,2,2],[3,1,2],[1,2]], and then call get(stacks, 1)\texttt{get(stacks, 1)}.
    • stacks[1]\texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1]\texttt{stacks[1]}, which makes stacks\texttt{stacks} become [[1,2],[3,1,2],[1,2]][[1,2],[3,1,2],[1,2]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1,2],[3,1],[1,2]][[1,2],[3,1],[1,2]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1,2],[3],[1,2]][[1,2],[3],[1,2]], and then call get(stacks, 1)\texttt{get(stacks, 1)}.
    • stacks[1]\texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1]\texttt{stacks[1]}, which makes stacks\texttt{stacks} become [[1],[3],[1,2]][[1],[3],[1,2]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is not empty, set \texttt{new_pos := 3}, and pop the top element of stacks[2]\texttt{stacks[2]}, which makes stacks\texttt{stacks} become [[1],[],[1,2]][[1],[],[1,2]], and then call get(stacks, 3)\texttt{get(stacks, 3)}.
    • stacks[3]\texttt{stacks[3]} is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[3]\texttt{stacks[3]}, which makes stacks\texttt{stacks} become [[1],[],[1]][[1],[],[1]], and then call get(stacks, 2)\texttt{get(stacks, 2)}.
    • stacks[2]\texttt{stacks[2]} is empty, return 22.

在第一个例子中:

  • 当调用 init(1)\texttt{init(1)} 时,设置 stacks := [[1,2,2],[3,1,2],[1,2,1]]\texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]},然后调用 get(stacks, 1)\texttt{get(stacks, 1)}。
    • stacks[1]\texttt{stacks[1]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[1]\texttt{stacks[1]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[3,1,2],[1,2,1]][[1,2],[3,1,2],[1,2,1]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[3,1],[1,2,1]][[1,2],[3,1],[1,2,1]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 1},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[3],[1,2,1]][[1,2],[3],[1,2,1]],然后调用 get(stacks, 1)\texttt{get(stacks, 1)}。
    • stacks[1]\texttt{stacks[1]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[1]\texttt{stacks[1]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1],[3],[1,2,1]][[1],[3],[1,2,1]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 3},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1],[],[1,2,1]][[1],[],[1,2,1]],然后调用 get(stacks, 3)\texttt{get(stacks, 3)}。
    • stacks[3]\texttt{stacks[3]} 非空,设 \texttt{new_pos := 1},并弹出 stacks[3]\texttt{stacks[3]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1],[],[1,2]][[1],[],[1,2]],然后调用 get(stacks, 1)\texttt{get(stacks, 1)}。
    • stacks[1]\texttt{stacks[1]} 非空,设 \texttt{new_pos := 1},并弹出 stacks[1]\texttt{stacks[1]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[],[],[1,2]][[],[],[1,2]],然后调用 get(stacks, 1)\texttt{get(stacks, 1)}。
    • stacks[1]\texttt{stacks[1]} 为空,返回 11。
  • 当调用 init(2)\texttt{init(2)} 时,设置 stacks := [[1,2,2],[3,1,2],[1,2,1]]\texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]},然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2,2],[3,1],[1,2,1]][[1,2,2],[3,1],[1,2,1]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 1},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2,2],[3],[1,2,1]][[1,2,2],[3],[1,2,1]],然后调用 get(stacks, 1)\texttt{get(stacks, 1)}。
    • stacks[1]\texttt{stacks[1]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[1]\texttt{stacks[1]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[3],[1,2,1]][[1,2],[3],[1,2,1]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 3},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[],[1,2,1]][[1,2],[],[1,2,1]],然后调用 get(stacks, 3)\texttt{get(stacks, 3)}。
    • stacks[3]\texttt{stacks[3]} 非空,设 \texttt{new_pos := 1},并弹出 stacks[3]\texttt{stacks[3]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[],[1,2]][[1,2],[],[1,2]],然后调用 get(stacks, 1)\texttt{get(stacks, 1)}。
    • stacks[1]\texttt{stacks[1]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[1]\texttt{stacks[1]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1],[],[1,2]][[1],[],[1,2]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 为空,返回 22。
  • 当调用 init(3)\texttt{init(3)} 时,设置 stacks := [[1,2,2],[3,1,2],[1,2,1]]\texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]},然后调用 get(stacks, 3)\texttt{get(stacks, 3)}。
    • stacks[3]\texttt{stacks[3]} 非空,设 \texttt{new_pos := 1},并弹出 stacks[3]\texttt{stacks[3]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2,2],[3,1,2],[1,2]][[1,2,2],[3,1,2],[1,2]],然后调用 get(stacks, 1)\texttt{get(stacks, 1)}。
    • stacks[1]\texttt{stacks[1]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[1]\texttt{stacks[1]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[3,1,2],[1,2]][[1,2],[3,1,2],[1,2]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[3,1],[1,2]][[1,2],[3,1],[1,2]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 1},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1,2],[3],[1,2]][[1,2],[3],[1,2]],然后调用 get(stacks, 1)\texttt{get(stacks, 1)}。
    • stacks[1]\texttt{stacks[1]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[1]\texttt{stacks[1]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1],[3],[1,2]][[1],[3],[1,2]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 非空,设 \texttt{new_pos := 3},并弹出 stacks[2]\texttt{stacks[2]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1],[],[1,2]][[1],[],[1,2]],然后调用 get(stacks, 3)\texttt{get(stacks, 3)}。
    • stacks[3]\texttt{stacks[3]} 非空,设 \texttt{new_pos := 2},并弹出 stacks[3]\texttt{stacks[3]} 的栈顶元素,使 stacks\texttt{stacks} 变为 [[1],[],[1]][[1],[],[1]],然后调用 get(stacks, 2)\texttt{get(stacks, 2)}。
    • stacks[2]\texttt{stacks[2]} 为空,返回 22。

输入解题思路,AI测评打分。不知道怎么写?

首页