loj#P3512. 「USACO 2021 US Open Platinum」Routing Schemes
「USACO 2021 US Open Platinum」Routing Schemes
Description
Consider a network of () nodes labeled . Each node is designated as a sender, a receiver, or neither. The number of senders, , is equal to the number of receivers ().
The connections between the nodes in this network can be described by a list of directed edges each of the form , meaning that node may route to node . Interestingly, all of these edges satisfy the property that , aside from that satisfy (). There are no self-loops (edges of the form ).
The description of a "routing scheme" consists of a set of directed paths from senders to receivers such that no two of these paths share an endpoint. That is, the paths connect distinct senders to distinct receivers. A path from a sender to a receiver can be described as a sequence of nodes
such that the directed edges exist for all . A node may appear more than once within the same path.
Count the number of distinct routing schemes such that every directed edge is traversed exactly once. Since the answer may be very large, report it modulo . It is guaranteed that there is at least one routing scheme satisfying these constraints.
Each input contains () test cases that should be solved independently. It is guaranteed that the sum of over all test cases does not exceed .
Input
The first line of the input contains , the number of test cases.
The first line of each test case contains the integers and . Note that is not explicitly given within the input.
The second line of each test case contains a string of length . The -th character of the string is equal to S
if the -th node is a sender, R
if the -th node is a receiver, or .
if the -th node is neither. The number of R
s in this string is equal to the number of S
s, and there is at least one S
.
The next lines of each test case each contain a bit string of zeros and ones. The -th bit of the -th line is equal to if there exists a directed edge from node to node , and otherwise. As there are no self-loops, the main diagonal of the matrix consists solely of zeros. Furthermore, there are exactly ones below the main diagonal.
Consecutive test cases are separated by newlines for readability.
Output
For each test case, the number of routing schemes such that every edge is traversed exactly once, modulo . It is guaranteed that there is at least one valid routing scheme for each test case.
2
8 0
SS....RR
00100000
00100000
00011000
00000100
00000100
00000011
00000000
00000000
13 0
SSS.RRRSS.RR.
0001000000000
0001000000000
0001000000000
0000111000000
0000000000000
0000000000000
0000000000000
0000000001000
0000000001000
0000000000110
0000000000000
0000000000000
0000000000000
4
12
2
5 1
SS.RR
00101
00100
10010
00000
00000
6 2
S....R
001000
000100
010001
000010
001000
000000
3
1
5
3 2
RS.
010
101
100
4 2
.R.S
0100
0010
1000
0100
4 2
.SR.
0000
0011
0100
0010
5 2
.SSRR
01000
10101
01010
00000
00000
6 2
SS..RR
001010
000010
000010
000010
100101
000000
2
1
2
6
24
Scoring
- Test cases 4-5 satisfy .
- Test cases 6-7 satisfy .
- Test cases 8-12 satisfy .
- Test cases 13-24 satisfy .
Problem credits: Benjamin Qi