CF1266H.Red-Blue Graph

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a directed graph on nn vertices numbered 11 through nn where each vertex (except nn ) has two outgoing arcs, red and blue. At any point in time, exactly one of the arcs is active for each vertex. Initially, all blue arcs are active and there is a token located at vertex 11 . In one second, the vertex with token first switches its active arcs — the inactive arc becomes active and vice versa. Then, the token is moved along the active arc. When the token reaches the vertex nn , it stops. It is guaranteed that nn is reachable via arcs from every vertex.

You are given qq queries. Each query contains a state of the graph — a pair (v,s)(v, s) of the following form:

  • vv is the vertex where the token is currently located;
  • ss is a string consisting of n1n - 1 characters. The ii -th character corresponds to the color of the active edge leading from the ii -th vertex (the character is 'R' if red arc is active, otherwise the character is 'B').

For each query, determine whether the given state is reachable from the initial state and the first time this configuration appears. Note that the two operations (change active arc and traverse it) are atomic — a state is not considered reached if it appears after changing the active arc but before traversing it.

输入格式

The first line contains a single integer nn ( 2n582 \leq n \leq 58 ) — the number of vertices.

n1n-1 lines follow, ii -th of contains two space separated integers bib_i and rir_i ( 1bi,rin1 \leq b_i, r_i \leq n ) representing a blue arc (i,bi)(i, b_i) and red arc (i,ri)(i, r_i) , respectively. It is guaranteed that vertex nn is reachable from every vertex.

The next line contains a single integer qq ( 1q50001 \leq q \leq 5000 ) — the number of queries.

Then qq lines with queries follow. The jj -th of these lines contains an integer vv ( 1v<n1 \leq v < n ) and a string ss of length n1n-1 consiting only of characters 'R' and 'B'. The ii -th of these characters is 'R' if the red arc going from ii is active and 'B' otherwise.

输出格式

Output qq lines, each containing answer to a single query.

If the state in the ii -th query is unreachable, output the integer 1-1 . Otherwise, output tit_i — the first time when the state appears (measured in seconds, starting from the initial state of the graph which appears in time 00 ).

输入输出样例

  • 输入#1

    6
    2 1
    5 5
    2 1
    6 3
    4 3
    21
    1 BBBBB
    1 RBBBB
    2 BBBBB
    5 BRBBB
    3 BRBBR
    1 BRRBR
    1 RRRBR
    2 BRRBR
    5 BBRBR
    4 BBRBB
    3 BBRRB
    2 BBBRB
    5 BRBRB
    3 BRBRR
    1 BRRRR
    1 RRRRR
    2 BRRRR
    5 BBRRR
    4 BBRRB
    2 BRBBB
    4 BRBBR
    

    输出#1

    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    -1
    -1
    

说明/提示

The graph in the first example is depticed in the figure below.

The first 1919 queries denote the journey of the token. On the 1919 -th move the token would reach the vertex 66 . The last two queries show states that are unreachable.

首页