luogu#P7736. [NOI2021] 路径交点

[NOI2021] 路径交点

Problem Description

Xiao L has a directed graph. The vertices in the graph can be divided into kk layers. Layer ii has nin_i vertices. The numbers of vertices in layer 11 and layer kk are the same, i.e., n1=nkn_1 = n_k. For layer jj (2jk12 \leq j \leq k-1), it holds that n1nj2n1n_1 \leq n_j \leq 2n_1. For vertices in layer jj (1j<k1 \leq j < k), all edges starting from them only go to vertices in layer j+1j + 1. There are no edges pointing to vertices in layer 11, and vertices in layer kk do not have outgoing edges to any other vertices.

Now Xiao L wants to choose n1n_1 paths from this graph. Each path starts at a vertex in layer 11 and ends at a vertex in layer kk. Also, it is required that each vertex in the graph appears in at most one path. More specifically, number the vertices in each layer as 1,2,,ni1,2,\ldots,n_i. Then each path can be written as a kk-tuple (p1,p2,,pk)(p_1,p_2,\ldots,p_k), meaning that this path passes through vertex pjp_j (1pjnj1 \leq p_j \leq n_j) in layer jj in order, and the vertex pjp_j in layer jj (1j<k1 \leq j < k) has an edge to vertex pj+1p_{j+1} in layer j+1j+1.

Xiao L drew these paths on paper and found that they produce some intersection points. For two paths P,QP,Q, let their edges between layer jj and layer j+1j+1 be (Pj,Pj+1)(P_j,P_{j+1}) and (Qj,Qj+1)(Q_j,Q_{j+1}), respectively. If

(PjQj)×(Pj+1Qj+1)<0(P_j-Q_j)\times(P_{j+1}-Q_{j+1})<0

then they are said to produce an intersection point after layer jj. The number of intersections between two paths is the total number of intersection points they produce after layers 1,2,,k11,2,\ldots,k-1. For the whole path selection (path scheme), its number of intersections is the sum of the numbers of intersections over all unordered pairs of distinct paths. For example, the figure below shows an example with 33 paths and 33 intersections, where the red points are intersections.

Xiao L now wants to know how many more path schemes with an even number of intersections there are than path schemes with an odd number of intersections. Two path schemes are considered the same if and only if their kk-tuples, written in the order of the starting vertex indices in the first layer, are identical. Since the final result may be very large, please output it modulo 998244353998244353 (a large prime).

Input Format

This problem contains multiple test cases. The first line contains a positive integer TT, indicating the number of test cases. For each test case:

The first line contains a positive integer kk, indicating that there are kk layers of vertices.

The second line contains kk integers n1,n2,,nkn_1,n_2,\ldots,n_k, indicating the number of vertices in each layer. It is guaranteed that n1=nkn_1=n_k, and n1ni2n1n_1 \leq n_i \leq 2n_1 (2ik12 \leq i \leq k-1).

The third line contains k1k-1 integers m1,m2,,mk1m_1,m_2,\ldots,m_{k-1}, indicating the number of edges from layer jj to layer j+1j+1. It is guaranteed that mjnj×nj+1m_j \leq n_j \times n_{j+1}.

Then there are k1k-1 blocks of input. The jj-th block (1j<k1 \leq j < k) contains mjm_j lines. Each line has two integers u,vu,v, meaning that vertex uu in layer jj has a directed edge to vertex vv in layer j+1j+1.

The input guarantees that there are no multiple edges in the graph.

Output Format

Output TT lines. Each line contains one integer, the answer for that test case modulo 998244353998244353.

1
3
2 3 2
4 4
1 1 
1 2
2 1
2 3
1 2
2 1
3 1
3 2
1

Hint

[Sample Explanation #1]

There are 22 schemes with an even number of intersections and 11 scheme with an odd number of intersections, so the answer is 11.

If you swap path 11 and path 22 in the table below, you will get the same scheme. For example, the scheme where path 11 is (2,3,1)(2, 3, 1) and path 22 is (1,1,2)(1, 1, 2) is the same as scheme 11, so it will not be counted separately.

Path scheme Path 11 Path 22 Total intersections
11 (1,1,2)(1,1,2) (2,3,1)(2,3,1) 11
22 (1,2,1)(1,2,1) (2,1,2)(2,1,2) 22
33 (2,3,2)(2,3,2) 00

[Sample #2]

See the attachments xpath2.in and xpath2.ans.

The constraints of this sample are consistent with test points 787 \sim 8.

[Sample #3]

See the attachments xpath3.in and xpath3.ans.

The constraints of this sample are consistent with test points 9109 \sim 10.

[Sample #4]

See the attachments xpath4.in and xpath4.ans.

The constraints of this sample are consistent with test points 141514 \sim 15.

[Constraints]

For all testdata: 2k1002 \leq k \leq 100, 2n11002 \leq n_1 \leq 100, 1T51 \leq T \leq 5.

In each test point, it is guaranteed that there is only 11 test case with n1>10n_1 > 10.

Test point ID k=k= n1n_1 \leq Special properties
141 \sim 4 22 1010 None
565 \sim 6 1010 A, B
787 \sim 8 A
9109 \sim 10 None
111311 \sim 13 22 100100
141514 \sim 15 100100 A, B
161716 \sim 17 A
182018 \sim 20 None

Special property A: For all ii (2ik12 \leq i \leq k-1), ni=n1n_i = n_1.

Special property B: It is guaranteed that the total number of path schemes is at most 11.

Translated by ChatGPT 5