Candy Delivery
Time limit3sMemory limit1024 MB
On a tree of N candy stores with five candy types, decide for each friend in order whether Yunjae, choosing his start freely, can walk a shortest path to the friend and pass a store selling the wanted candy.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Prefix sum, Linked list
- Solved
- No attempts yet
Problem
The village where Yunjae lives has candy stores. The stores are numbered from to . Each store sells only one of five kinds of candy. There is also a road network of roads connecting the candy stores. Every road takes unit of time to traverse, and using the roads one can travel from any candy store to every other candy store.
Today Yunjae plans to meet friends one at a time, in order. On the way to meet each friend, he must buy the kind of candy that friend likes. If he fails, the friend gets angry and leaves.
To meet his friends quickly, Yunjae sets off for the next friend immediately after meeting one friend. To save as much time as possible, he always travels along a shortest path, and along the way he stops at candy stores he passes and buys candy there. He may also buy candy at a store located where the friend is. However, his pockets are small, so he can carry only one candy at a time. That is, while on the way to meet some friend A, he cannot buy a candy for another friend B in advance.
To find out in advance which friends will leave him, Yunjae wants to know whether he can buy candy for each friend. Yunjae may choose his starting location freely. Write a program that helps Yunjae determine, for each friend, whether he can buy the kind of candy that friend likes.
Input
The first line gives an integer , the number of candy stores. ()
The second line gives integers separated by spaces, indicating the kind of candy sold at each candy store. A candy kind is a positive integer between and .
From the third line, lines give road information as pairs . This means there is a bidirectional road connecting candy store and candy store . (, )
The next line gives an integer , the number of friends. ()
The next lines give, in order of earliest appointment time, the number of the store where each friend stands and the kind of candy that friend likes. Several friends may stand in front of the same store.
Output
In order of earliest appointment time, print PLAY on its own line if Yunjae can buy the kind of candy that friend likes, and CRY otherwise.