Warehouses left out of a loop route
Statement
A logistics company has n warehouses, and the warehouses are numbered 1 through n. For each warehouse the company has settled on one next warehouse to pass goods to. A warehouse may name itself as its next warehouse, and several warehouses may name the same warehouse as their next one.
When goods are sent from some warehouse they pass on to its next warehouse, and from there on to that warehouse's next one, and keep moving. Sometimes the goods come back round to the warehouse they first started from. A set of warehouses whose goods go round and come back to where they started is called a loop route.
For example, if warehouse 4 passes goods to warehouse 7, warehouse 7 passes to warehouse 6 and warehouse 6 passes to warehouse 4, these three warehouses make one loop route. And if warehouse 3 passes goods on to warehouse 3, which is itself, warehouse 3 makes a loop route on its own. If warehouse 2 passes to warehouse 1 and warehouse 1 passes to warehouse 3, on the other hand, the goods sent from warehouse 2 travel as far as warehouse 3 and only stay around there, never coming back to warehouse 2. So warehouses 1 and 2 are left out of every loop route.
Write a program that, given the next warehouse each warehouse passes goods to, works out the number of warehouses left out of every loop route. The company runs this survey in several regions, so the information for T different regions is given and the answer has to be found for each region.
For example, suppose the next warehouse each warehouse passes goods to in a region with n=7 is as in the table below.
| Warehouse number | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Next warehouse | 3 | 1 | 3 | 7 | 3 | 4 | 6 |
Here warehouse 3 passes goods to itself, so it makes a loop route on its own. Warehouse 4 passes to warehouse 7, warehouse 7 passes to warehouse 6 and warehouse 6 passes to warehouse 4, so those three warehouses make one loop route. For the remaining warehouses 1, 2 and 5, the goods they send run into warehouse 3 and stop there, so they are left out of every loop route.
| Warehouse number | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| In a loop route | Left out | Left out | In | In | Left out | In | In |
So three warehouses, 1, 2 and 5, are left out of every loop route, and the answer for this region is 3.
Input and Output Examples
Example 1
Input
2 7 3 1 3 7 3 4 6 3 1 2 3
Output
3 0
Example 2
Input
1 3 3 2 2
Output
2
Example 3
Input
1 4 1 3 2 1
Output
1
Input
First the number of regions T is given on the first line. T is a natural number from 1 to 3.
Next the information for the T regions is given in order.
The first line of each region holds the number of warehouses n. n is a natural number from 2 to 5,000.
Next the second line holds the numbers of the next warehouses that warehouses 1 through n pass goods to, in order, separated by spaces. Each number is a natural number from 1 to n.
Output
For the T regions, print the number of warehouses left out of every loop route, one per line, in order. Keep the order in which the regions were given. If every warehouse is in a loop route, print 0.
Requirements
- Each warehouse settles on exactly one next warehouse to pass goods to
- A warehouse that names itself as its next warehouse makes a loop route on its own
- Several warehouses may name the same warehouse as their next one
- Print the answer for each region, one per line
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 3 3 2 2
2
1 4 1 3 2 1
1
2 7 3 1 3 7 3 4 6 3 1 2 3
3 0
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.