CF1889D.Game of Stacks
普及/提高-
通过率:0%

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.
输入格式
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 .
输出格式
You need to output n values, the i -th of which is the value returned by 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 .