luogu#P3083. [USACO13OPEN] Luxury River Cruise S

[USACO13OPEN] Luxury River Cruise S

Problem Description

Farmer John is taking Bessie and the cows on a cruise! They are sailing on a network of rivers with ‌NN‌ ports (1N1,000)(1 \leq N \leq 1,000) labeled 1N1 \dots N, and Bessie starts at port 11. Each port has exactly two rivers leading out of it which lead directly to other ports, and rivers can only be sailed one way.

At each port, the tour guides choose either the "left" river or the "right" river to sail down next, but they keep repeating the same choices over and over. More specifically, the tour guides have chosen a short sequence of ‌MM‌ directions (1M500)(1 \leq M \leq 500), each either "left" or "right", and have repeated it ‌KK‌ times (1K109)(1 \leq K \leq 10^9). Bessie thinks she is going in circles — help her figure out where she ends up!

Input Format

  • Line 11: Three space-separated integers ‌N,MN, M, and kk.

  • Lines 2N+12 \dots N+1: Line i+1i+1 has two space-separated integers, representing the number of the ports that port ii's left and right rivers lead to, respectively.

  • Line N+2N+2: MM space-separated characters, either L or R. L represents a choice of 'left' and R represents a choice of 'right'.

Output Format

  • Line 11: A single integer giving the number of the port where Bessie's cruise ends.
4 3 3 
2 4 
3 1 
4 2 
1 3 
L L R 

4 

Hint

INPUT DETAILS

The port numbers are arranged clockwise in a circle, with L being a clockwise rotation and R being a counterclockwise rotation. The sequence taken is LLRLLRLLR.

OUTPUT DETAILS

After the first iteration of the sequence of directions, Bessie is at port 22 (1232)(1 \rightarrow 2 \rightarrow 3 \rightarrow 2); after the second, she is at port 33 (2343)(2 \rightarrow 3 \rightarrow 4 \rightarrow 3), and at the end she is at port 44 (3414)(3 \rightarrow 4 \rightarrow 1 \rightarrow 4).