Cities
InterviewTime limit1sMemory limit128 MB
Count for each city on a directed line how many other cities are reachable through one-way and two-way roads.
- Level
Medium5 of 10
- Topics
- Array, Prefix sum
- Solved
- No attempts yet
Problem
There are cities lined up along a river. Between every pair of adjacent cities exactly one road is built, but not every road is two-way, so you cannot always travel from a given city to all of the others.
Given which roads are built, determine for each city how many other cities are reachable from it.
Input
The first line contains one integer (), the number of cities.
The second line contains integers (), where describes the road between city and city :
- if , there is a one-way road from city to city ;
- if , there is a one-way road from city to city ;
- if , the two cities are joined by a two-way road.
Output
Print integers on a single line, separated by spaces, where is the number of cities reachable from city , not counting city itself.