#P3825. [NOI2017] 游戏

    ID: 2782 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017NOI 系列Special JudgeO2优化拓扑排序2-SAT强连通分量,缩点

[NOI2017] 游戏

Description

Little L plans to play nn games. Each game uses one map, and Little L will choose a car to complete the game on that map.

Little L has three cars, denoted by uppercase letters AA, BB, and CC. There are four types of maps, denoted by lowercase letters xx, aa, bb, and cc.

Among them, car AA is not suitable for map aa, car BB is not suitable for map bb, car CC is not suitable for map cc, while map xx is suitable for all cars.

Maps that are suitable for all cars are rare; there will be at most dd of them.

The maps for the nn games can be described by a string of lowercase letters. For example, S=xaabxcbcS=\texttt{xaabxcbc} means Little L plans to play 88 games: the 11-st and 55-th maps are of type xx, suitable for all cars; the 22-nd and 33-rd maps are of type aa, unsuitable for car AA; the 44-th and 77-th maps are of type bb, unsuitable for car BB; and the 66-th and 88-th maps are of type cc, unsuitable for car CC.

Little L has some special requirements, which can be described by a quadruple (i,hi,j,hj)(i, h_i, j, h_j), meaning: if the car used in game ii is hih_i, then the car used in game jj must be hjh_j.

Can you help Little L choose a car for each game? If there are multiple solutions, output any of them.

If there is no solution, output -1.

Input Format

The first line contains two non-negative integers, nn and dd.

The second line contains a string SS.

The meanings of nn, dd, and SS are as described above. The string SS contains nn characters, and exactly dd of them are the lowercase letter xx.

The third line contains a positive integer mm, indicating that there are mm car-usage rules.

The next mm lines each contain a quadruple i,hi,j,hji, h_i, j, h_j, where ii and jj are integers, and hih_i and hjh_j are characters among AA, BB, and CC, with meanings as described above.

Output Format

Output one line.

If there is no solution, output -1.

If there is a solution, output a string of length nn consisting only of uppercase letters AA, BB, and CC, indicating how Little L arranges the cars for these nn games. If multiple solutions exist, output any one of them.

3 1
xcc
1
1 A 2 B
ABA

Hint

Sample 1 Explanation

Little LL plans to play 33 games. The 11-st map is of type xx, suitable for all cars, and the 22-nd and 33-rd maps are of type cc, unsuitable for car CC.

Little LL requires: if car AA is used in game 11, then car BB must be used in game 22.

Assigning cars AA, BB, AA to the 33 games satisfies all conditions.

Assigning cars BBBB B B or BAAB A A to the 33 games also satisfies all conditions and is considered correct.

However, assigning cars AABA A B or ABCA B C does not satisfy all conditions, so it is not considered correct.

Sample 2

See the attached file.

Constraints

Test point ID nn dd mm Other properties
11 2\le 2 00 4\le 4 None
22 n\le n
33 5\le 5 00 10\le 10
44 n\le n
55 10\le 10 00 20\le 20
66 8\le 8
77 20\le 20 00 40\le 40 SS only contains cc
88 None
99 8\le 8 SS only contains xx or cc
1010 None
1111 100\le 100 00 200\le 200 SS only contains cc
1212 None
1313 8\le 8 SS only contains xx or cc
1414 None
1515 5×103\le 5\times 10^3 00 104\le 10^4
1616 8\le 8 SS only contains xx or cc
1717 None
1818 5×104\le 5\times 10^4 00 105\le 10^5
1919 8\le 8 SS only contains xx or cc
2020 None

Translated by ChatGPT 5