CF1889D.Game of Stacks

普及/提高-

通过率:0%

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.

输入格式

The first line of the input contains one integer nn ( 1n1051\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 ( 0ki1050\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} ( 1ci,jn1\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, ki106\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)} .

输入输出样例

  • 输入#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 .
首页