SHADES
A map is given on which cities are marked, connected by a network of one-way roads. Some cities are rural and are marked on the map with white color. Some other cities are urban and are marked on the map with black color. Finally, cities that are neither rural nor urban are marked on the map with gray color.
Rural cities produce food, and urban cities consume it. Consequently, food travels from rural to urban cities along the roads. Cities through which food passes on its route are considered lucky because the economy flourishes there.
Specifically, a city is considered lucky if it is located on a route that starts from a rural city (white), ends in an urban city (black), and does not contain any other urban city except the final one (otherwise, the food would be at risk of being eaten on the way and might not travel until the end of the route). Note that on such a route, both the initial rural city and the final urban city are considered lucky. Also, note that there may be other rural cities between the initial rural city and the final urban city.
You are asked to find which cities are lucky.
Input
The first line of input will contain two integers and , separated by a single space: the number of cities and the number of roads. The second line will contain integers separated in pairs by a single space, each of which will be , , or . Each such value corresponds in order to a city. If the value is , the city is rural (white), if it is then the city is urban (black), and if it is then it is neither rural nor urban (gray). Each of the next lines will contain a pair of numbers , , with values between and inclusive. This pair means that there is a road that goes from city to city . In each road, , but there may be more roads between the same cities.
Output
The output should consist of a single line containing exactly integers separated in pairs by a single space. Each number corresponds to a city, in order, and will be if that city is lucky, otherwise .
Constraints
.
Execution time limit: 2 sec.
Memory limit: 128 MB.
Example of Input - Output
STDIN (shades.in)
4 5
0 2 1 2
1 2
2 3
3 4
4 1
2 3
STDOUT (shades.out)
1 1 1 0
Explanation
On the path , the initial city is rural (white) and the final one is urban (black), without any other urban (black) city in between. Therefore, all three of these cities are lucky. There is no path with this property that includes city , so it is not lucky.
Comments