#P3083. [USACO13OPEN] Luxury River Cruise S

[USACO13OPEN] Luxury River Cruise S

Description

Farmer John is taking Bessie and the other cows on a cruise! They are sailing on a river network with NN ports (1N1031 \le N \le 10^3), numbered 1,2,3,,N1, 2, 3, \dots, N, and Bessie starts at port 11. Each port has exactly two outgoing rivers that lead directly to other ports, and these rivers can be traversed only in one direction.

At each port, the guide chooses to take the left river or the right river to continue downstream, and they repeat the same sequence of choices over and over. More specifically, the guide has fixed in advance a direction sequence of length MM (1M5001 \le M \le 500), each direction being either left or right, and then repeats this sequence KK times (1K1091 \le K \le 10^9). Please help compute the port where Bessie finally stops.

Input Format

Line 11: Three space-separated integers NN, MM, KK.

Lines 22 to N+1N+1: Line i+1i+1 contains two space-separated integers, the indices of the ports reached by taking the left and right rivers from port ii.

Line N+2N+2: MM space-separated characters, each either L or R. L means take the left river, and R means take the right river.

Output Format

Output a single integer, the index of the port where Bessie’s cruise ends.

4 3 3 
2 4 
3 1 
4 2 
1 3 
L L R 

4 

Hint

In the sample, after finishing the first pass of the direction sequence, Bessie arrives at port 22 (path: 12321 \to 2 \to 3 \to 2);
after the second pass, she arrives at port 33 (path: 23432 \to 3 \to 4 \to 3);
finally, she ends at port 44 (path: 34143 \to 4 \to 1 \to 4).

Thanks to

https://www.luogu.com.cn/user/995934
anslation.

Translated by ChatGPT 5