codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1249B1.md (2524B)


      1 Source: https://codeforces.com/problemset/problem/1249/B1
      2 
      3 B1. Books Exchange (easy version)
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 The only difference between easy and hard versions is constraints.
     22 
     23 There are $n$ kids, each of them is reading a unique book. At the end of any day, the $i$-th kid will give his book to the $p_i$-th kid (in case of $i = p_i$ the kid will give his book to himself). It is guaranteed that all values of $p_i$ are distinct integers from $1$ to $n$ (i.e. $p$ is a permutation). The sequence $p$ doesn't change from day to day, it is fixed.
     24 
     25 For example, if $n=6$ and $p=[4, 6, 1, 3, 5, 2]$ then at the end of the first day the book of the $1$-st kid will belong to the $4$-th kid, the $2$-nd kid will belong to the $6$-th kid and so on. At the end of the second day the book of the $1$-st kid will belong to the $3$-th kid, the $2$-nd kid will belong to the $2$-th kid and so on.
     26 
     27 Your task is to determine the number of the day the book of the $i$-th child is returned back to him for the first time for every $i$ from $1$ to $n$.
     28 
     29 Consider the following example: $p = [5, 1, 2, 4, 3]$. The book of the $1$-st kid will be passed to the following kids:
     30 
     31 * after the $1$-st day it will belong to the $5$-th kid,
     32 * after the $2$-nd day it will belong to the $3$-rd kid,
     33 * after the $3$-rd day it will belong to the $2$-nd kid,
     34 * after the $4$-th day it will belong to the $1$-st kid.
     35 
     36 So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.
     37 
     38 You have to answer $q$ independent queries.
     39 
     40 Input
     41 
     42 The first line of the input contains one integer $q$ ($1 \le q \le 200$) — the number of queries. Then $q$ queries follow.
     43 
     44 The first line of the query contains one integer $n$ ($1 \le n \le 200$) — the number of kids in the query. The second line of the query contains $n$ integers $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$, all $p_i$ are distinct, i.e. $p$ is a permutation), where $p_i$ is the kid which will get the book of the $i$-th kid.
     45 
     46 Output
     47 
     48 For each query, print the answer on it: $n$ integers $a_1, a_2, \dots, a_n$, where $a_i$ is the number of the day the book of the $i$-th child is returned back to him for the first time in this query.
     49 
     50 Example
     51 
     52 Input
     53 
     54 ```
     55 6
     56 5
     57 1 2 3 4 5
     58 3
     59 2 3 1
     60 6
     61 4 6 2 1 5 3
     62 1
     63 1
     64 4
     65 3 4 1 2
     66 5
     67 5 1 2 4 3
     68 ```
     69 
     70 Output
     71 
     72 ```
     73 1 1 1 1 1 
     74 3 3 3 
     75 2 3 3 2 1 3 
     76 1 
     77 2 2 2 2 
     78 4 4 4 1 4
     79 ```