Arezou and her brother Borzou are twins. They have received an amazing toy train set for their birthday, and they used it to build a railway system with stations and one-way tracks. The stations are numbered from to . Each track starts at one station and ends at the same or a different station. There is at least one track starting at each station.
Some stations are charging stations. Whenever the train arrives at a charging station, it gets fully charged. A fully charged train has enough energy to travel along consecutive tracks. That is, the train runs out of energy just when it enters the track after last being charged.
On each station there is a switch that can be pointed to any of the tracks that start at that station. When a train is at a station, it leaves it using the track that is pointed to by the switch on that station.
The twins are going to play a game with their train. They have already divided all the stations between themselves: each station is either owned by Arezou or by Borzou. There is a single train. At the beginning of the game the train is at station and it is fully charged. To start the game, the owner of station points the switch on station to one of the tracks that start at station . Then, they turn the train on and the train starts traveling along the tracks.
Whenever the train enters a station for the first time, the owner of that station sets the switch on that station. Once a switch is set, it stays in the same position for the rest of the game. Thus, if a train re-enters a station it visited before, it will leave that station along the same track as before.
Since there is a finite number of stations, the train will eventually start going along a cycle. A cycle is a sequence of distinct stations such that the train leaves station (for ) using a track going to station , and it leaves station using a track going to station . Note that a cycle may consist of a single station (i.e., have ) if the train leaves the station using a track that goes back to .
Arezou wins the game if the train continues going indefinitely, and Borzou wins if the train runs out of energy. In other words, if there is at least one charging station among , the train can recharge and cycle indefinitely, and Arezou wins. Otherwise, it will run out of energy (possibly after turning around the cycle several times), and Borzou wins.
You are given the description of the railway system. Arezou and Borzou are going to play games. In the game, for , the train will initially be at station . Your task is to find, for each game, whether there is a strategy for Arezou that guarantees she wins, regardless of how Borzou plays.
Implementation details
You should implement the following procedure:
std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v)
- : array of length . If Arezou owns station , . Otherwise, Borzou owns station and .
- : array of length . If the station is a charging station, . Otherwise, .
- and : arrays of length . For all , there is a one-way track starting at station and ending at station .
- This procedure should return an array of length . For each , the value of should be if Arezou can win the game that starts at station , regardless of how Borzou plays. Otherwise, the value of should be .
Example
who_wins({0, 1}, {1, 0}, {0, 0, 1, 1}, {0, 1, 0, 1})
- There are stations. Borzou is the owner of station , which is a charging station. Arezou is the owner of station , which is not a charging station.
- There are tracks , , , and , where denotes a one-way track from station to station .
- Consider the game in which the train is initially placed at station . If Borzou sets the switch on station towards the track , the train will indefinitely cycle through this track (note that station is a charging station). In this case, Arezou wins. Otherwise, if Borzou sets the switch on station towards track , Arezou can set the switch on station towards . If this happens, the train will indefinitely cycle through both stations. Again Arezou wins, since station is a charging station and the train will not stop. Hence, Arezou can win the game, regardless of what Borzou does.
- By a similar reasoning, in the game starting at station Arezou can also win, regardless of how Borzou plays. Thus, the procedure should return .
Constraints
- .
- .
- There is at least one charging station.
- There is at least one track starting at each station.
- There might be tracks that start and end at the same station (i.e., ).
- Each track is distinct. In other words, there are no such two indices and that and .
- (for all ).
Subtasks
- (5 points) For all , either or .
- (10 points) .
- (11 points) Arezou owns all stations.
- (11 points) Borzou owns all stations.
- (12 points) There is exactly one charging station.
- (51 points) No additional constraints.
Sample grader
The sample grader reads the input in the following format:
- line :
- line :
- line :
- line (for ):
The sample grader prints the return value of who_wins
in the following format:
- line :
Comments