CF1889D.Game of Stacks
NOI/NOI+/CTSC
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have n stacks r1,r2,…,rn. Each stack contains some positive integers ranging from 1 to n.
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).
Note that, during these calls, the stacks r1,r2,…,rn don't change, so the calls init(1),init(2),…,init(n) are independent.
你有 n 个栈 r1,r2,…,rn。每个栈中包含若干个取值在 1 到 n 之间的正整数。
定义如下函数:
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) 各自返回的值。
注意:在这些调用过程中,栈 r1,r2,…,rn 均不发生改变,因此调用 init(1),init(2),…,init(n) 是相互独立的。
输入格式
The first line of the input contains one integer n (1≤n≤105) — the length of the array r.
Each of the following n lines contains several integers. The first integer ki (0≤ki≤105) represents the number of elements in the i-th stack, and the following ki positive integers ci,1,ci,2,…,ci,ki (1≤ci,j≤n) represent the elements in the i-th stack. ci,1 is the bottom element.
In each test, ∑ki≤106.
输入的第一行包含一个整数 n(1≤n≤105)—— 表示数组 r 的长度。
接下来的 n 行,每行包含若干个整数。第一个整数 ki(0≤ki≤105)表示第 i 个栈中元素的个数;随后的 ki 个正整数 ci,1,ci,2,…,ci,ki(1≤ci,j≤n)表示第 i 个栈中的元素,其中 ci,1 为栈底元素。
在每个测试用例中,满足 ∑ki≤106。
输出格式
You need to output n values, the i-th of which is the value returned by init(i).
你需要输出 n 个值,其中第 i 个值为 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), set stacks := [[1,2,2],[3,1,2],[1,2,1]], and then call get(stacks, 1).
- stacks[1] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1], which makes stacks become [[1,2],[3,1,2],[1,2,1]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[2], which makes stacks become [[1,2],[3,1],[1,2,1]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[2], which makes stacks become [[1,2],[3],[1,2,1]], and then call get(stacks, 1).
- stacks[1] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1], which makes stacks become [[1],[3],[1,2,1]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 3}, and pop the top element of stacks[2], which makes stacks become [[1],[],[1,2,1]], and then call get(stacks, 3).
- stacks[3] is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[3], which makes stacks become [[1],[],[1,2]], and then call get(stacks, 1).
- stacks[1] is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[1], which makes stacks become [[],[],[1,2]], and then call get(stacks, 1).
- stacks[1] is empty, return 1.
- When you call init(2), set stacks := [[1,2,2],[3,1,2],[1,2,1]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[2], which makes stacks become [[1,2,2],[3,1],[1,2,1]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[2], which makes stacks become [[1,2,2],[3],[1,2,1]], and then call get(stacks, 1).
- stacks[1] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1], which makes stacks become [[1,2],[3],[1,2,1]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 3}, and pop the top element of stacks[2], which makes stacks become [[1,2],[],[1,2,1]], and then call get(stacks, 3).
- stacks[3] is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[3], which makes stacks become [[1,2],[],[1,2]], and then call get(stacks, 1).
- stacks[1] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1], which makes stacks become [[1],[],[1,2]], and then call get(stacks, 2).
- stacks[2] is empty, return 2.
- When you call init(3), set stacks := [[1,2,2],[3,1,2],[1,2,1]], and then call get(stacks, 3).
- stacks[3] is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[3], which makes stacks become [[1,2,2],[3,1,2],[1,2]], and then call get(stacks, 1).
- stacks[1] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1], which makes stacks become [[1,2],[3,1,2],[1,2]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[2], which makes stacks become [[1,2],[3,1],[1,2]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 1}, and pop the top element of stacks[2], which makes stacks become [[1,2],[3],[1,2]], and then call get(stacks, 1).
- stacks[1] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[1], which makes stacks become [[1],[3],[1,2]], and then call get(stacks, 2).
- stacks[2] is not empty, set \texttt{new_pos := 3}, and pop the top element of stacks[2], which makes stacks become [[1],[],[1,2]], and then call get(stacks, 3).
- stacks[3] is not empty, set \texttt{new_pos := 2}, and pop the top element of stacks[3], which makes stacks become [[1],[],[1]], and then call get(stacks, 2).
- stacks[2] is empty, return 2.
在第一个例子中:
- 当调用 init(1) 时,设置 stacks := [[1,2,2],[3,1,2],[1,2,1]],然后调用 get(stacks, 1)。
- stacks[1] 非空,设 \texttt{new_pos := 2},并弹出 stacks[1] 的栈顶元素,使 stacks 变为 [[1,2],[3,1,2],[1,2,1]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 2},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1,2],[3,1],[1,2,1]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 1},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1,2],[3],[1,2,1]],然后调用 get(stacks, 1)。
- stacks[1] 非空,设 \texttt{new_pos := 2},并弹出 stacks[1] 的栈顶元素,使 stacks 变为 [[1],[3],[1,2,1]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 3},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1],[],[1,2,1]],然后调用 get(stacks, 3)。
- stacks[3] 非空,设 \texttt{new_pos := 1},并弹出 stacks[3] 的栈顶元素,使 stacks 变为 [[1],[],[1,2]],然后调用 get(stacks, 1)。
- stacks[1] 非空,设 \texttt{new_pos := 1},并弹出 stacks[1] 的栈顶元素,使 stacks 变为 [[],[],[1,2]],然后调用 get(stacks, 1)。
- stacks[1] 为空,返回 1。
- 当调用 init(2) 时,设置 stacks := [[1,2,2],[3,1,2],[1,2,1]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 2},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1,2,2],[3,1],[1,2,1]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 1},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1,2,2],[3],[1,2,1]],然后调用 get(stacks, 1)。
- stacks[1] 非空,设 \texttt{new_pos := 2},并弹出 stacks[1] 的栈顶元素,使 stacks 变为 [[1,2],[3],[1,2,1]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 3},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1,2],[],[1,2,1]],然后调用 get(stacks, 3)。
- stacks[3] 非空,设 \texttt{new_pos := 1},并弹出 stacks[3] 的栈顶元素,使 stacks 变为 [[1,2],[],[1,2]],然后调用 get(stacks, 1)。
- stacks[1] 非空,设 \texttt{new_pos := 2},并弹出 stacks[1] 的栈顶元素,使 stacks 变为 [[1],[],[1,2]],然后调用 get(stacks, 2)。
- stacks[2] 为空,返回 2。
- 当调用 init(3) 时,设置 stacks := [[1,2,2],[3,1,2],[1,2,1]],然后调用 get(stacks, 3)。
- stacks[3] 非空,设 \texttt{new_pos := 1},并弹出 stacks[3] 的栈顶元素,使 stacks 变为 [[1,2,2],[3,1,2],[1,2]],然后调用 get(stacks, 1)。
- stacks[1] 非空,设 \texttt{new_pos := 2},并弹出 stacks[1] 的栈顶元素,使 stacks 变为 [[1,2],[3,1,2],[1,2]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 2},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1,2],[3,1],[1,2]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 1},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1,2],[3],[1,2]],然后调用 get(stacks, 1)。
- stacks[1] 非空,设 \texttt{new_pos := 2},并弹出 stacks[1] 的栈顶元素,使 stacks 变为 [[1],[3],[1,2]],然后调用 get(stacks, 2)。
- stacks[2] 非空,设 \texttt{new_pos := 3},并弹出 stacks[2] 的栈顶元素,使 stacks 变为 [[1],[],[1,2]],然后调用 get(stacks, 3)。
- stacks[3] 非空,设 \texttt{new_pos := 2},并弹出 stacks[3] 的栈顶元素,使 stacks 变为 [[1],[],[1]],然后调用 get(stacks, 2)。
- stacks[2] 为空,返回 2。
输入解题思路,AI测评打分。不知道怎么写?