This page is still under construction.

Parts of this page are still being built. What you see may change.

Luxury River Cruise

Interview

Time limit1sMemory limit128 MB

Summary
Each of N ports has a left and right outgoing river; follow a fixed direction string of length M repeated K times from port 1 and report the final port.
Level

Medium6 of 10

Topics
Binary search, Graph, Simulation, Implementation
Solved
No attempts yet

Problem

Farmer John is taking Bessie and the cows on a cruise! They are sailing on a network of rivers with NN ports (1≤N≤10001 \le N \le 1000) labeled 1…N1 \dots N, and Bessie starts at port 11. Each port has exactly two rivers leading out of it, going directly to two other ports, and each river can be sailed in only one direction.

At each port the tour guides pick either the left river or the right river to sail down next, and they repeat the same pattern over and over. Concretely, the guides fix a short sequence of MM directions (1≤M≤5001 \le M \le 500), each either left or right, and they follow this whole sequence KK times (1≤K≤1091 \le K \le 10^9). Bessie feels like she is going in circles -- help her work out which port she finishes at!

Input

  • Line 1: three space-separated integers NN, MM, and KK.
  • Lines 2 to N+1N+1: line i+1i+1 contains two space-separated integers -- the ports that port ii's left river and right river lead to, respectively.
  • Line N+2N+2: MM space-separated characters, each either L or R. L means the left river and R means the right river.

Output

  • A single integer: the number of the port where Bessie's cruise ends.

Hint

In the sample, the port numbers are arranged clockwise around a circle, L is a clockwise step and R is a counterclockwise step, and the sequence followed is L L R repeated three times.

After the first full pass through the direction sequence Bessie is at port 22 (1→2→3→21 \to 2 \to 3 \to 2); after the second pass she is at port 33 (2→3→4→32 \to 3 \to 4 \to 3); and after the third pass she finishes at port 44 (3→4→1→43 \to 4 \to 1 \to 4).

Examples2

  1. Example 1

    Input
    4 3 3
    2 4
    3 1
    4 2
    1 3
    L L R
    
    Expected output
    4
    
  2. Example 2

    Input
    4 3 1
    2 4
    3 1
    4 2
    1 3
    L L R
    
    Expected output
    2