#P4489. [CTSC2009] 纷繁世界

[CTSC2009] 纷繁世界

Description

For simplicity, assume there are NN people and MM types of events. Define a binary relation “related” between event types:

  • The relation is binary, meaning we only determine whether two types of events are related.
  • Events of the same type are always related.
  • If event xx is related to event yy, then event yy is also related to event xx.

Let Qi=(Qi,1,Qi,2,,Qi,Ci)Q_i = (Q_{i, 1}, Q_{i, 2}, \cdots, Q_{i, C_i}) be the sequence of events person ii plans to complete (called the plan sequence), where CiC_i is the length of QiQ_i. Each event Qi,jQ_{i, j} is one of the MM event types, and events of the same type may occur multiple times.

As time goes by, each event in every plan sequence will occur exactly once, and no two events occur at the same moment.

To describe the order in which events occur, define $P = (Q_{i_1, j_1}, Q_{i_2, j_2}, \cdots, Q_{i_l, j_l})$ as a development trajectory of the world. PP is an ordered sequence satisfying:

  1. For every person, each event Qi,jQ_{i, j} in their plan sequence appears in PP exactly once.
  2. For any two events Qi,j1Q_{i, j_1} and Qi,j2Q_{i, j_2} belonging to the same plan sequence (1j1<j2Ci1 \leq j_1 < j_2 \leq C_i), Qi,j1Q_{i, j_1} must occur before Qi,j2Q_{i, j_2} (i.e., appear earlier in PP).

Two trajectories P1P_1 and P2P_2 are defined to be essentially different if and only if there exist two related events Qi,jQ_{i, j} and Qu,vQ_{u, v} whose relative order differs in P1P_1 and P2P_2. In other words, if in P1P_1 the event Qi,jQ_{i, j} occurs before Qu,vQ_{u, v} while in P2P_2 the event Qi,jQ_{i, j} occurs after Qu,vQ_{u, v}, then P1P_1 and P2P_2 are essentially different; similarly, if in P1P_1 Qi,jQ_{i, j} occurs after Qu,vQ_{u, v} but in P2P_2 it occurs before Qu,vQ_{u, v}, then P1P_1 and P2P_2 are also essentially different.

Note: “Essentially the same” is transitive. That is, if P1P_1 is essentially the same as P2P_2, and P2P_2 is essentially the same as P3P_3, then P1P_1 is necessarily essentially the same as P3P_3.

Given NN, MM, each person’s plan sequence, and the relatedness between event types, compute how many essentially different world development trajectories there are.

Input Format

  • The first line contains an integer denoting the number of people NN.
  • The second line contains an integer denoting the number of event types MM. All event types are numbered from 00 to M1M - 1.
  • Then, for each person ii, the description of their plan sequence is given:
    • The first line contains one integer, the sequence length CiC_i.
    • The second line contains CiC_i integers, giving each event Qi,jQ_{i, j} in QiQ_i in order.
  • Finally, MM lines follow, containing an M×MM \times M matrix dep that describes the relatedness. Each line contains MM integers, each being 00 or 11. dep(i, j) denotes the integer in the ii-th row (from top to bottom) and the jj-th column (from left to right). If dep(i, j) equals 11, then event type ii and event type jj are related; otherwise, they are unrelated.

Output Format

Output a single line containing one integer, the number TT of essentially different world trajectories.

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

Hint

Sample Explanation

There are 22 people and 33 event types, with C1=C2=2C_1 = C_2 = 2. Q1,0=0Q_{1,0} = 0, Q1,1=1Q_{1,1} = 1, Q2,0=2Q_{2,0} = 2, Q2,1=1Q_{2,1} = 1.

There are 44 different development trajectories:

  • P1=(Q1,0,Q1,1,Q2,0,Q2,1)P_1 = (Q_{1,0}, Q_{1,1}, Q_{2,0}, Q_{2,1});
  • P2=(Q1,0,Q2,0,Q1,1,Q2,1)P_2 = (Q_{1,0}, Q_{2,0}, Q_{1,1}, Q_{2,1});
  • P3=(Q1,0,Q2,0,Q2,1,Q1,1)P_3 = (Q_{1,0}, Q_{2,0}, Q_{2,1}, Q_{1,1});
  • P4=(Q2,0,Q2,1,Q1,0,Q1,1)P_4 = (Q_{2,0}, Q_{2,1}, Q_{1,0}, Q_{1,1}).

Any other valid development trajectory is necessarily essentially the same as one of these four. For example, P=(Q2,0,Q1,0,Q2,1,Q1,1)P = (Q_{2,0}, Q_{1,0}, Q_{2,1}, Q_{1,1}) is essentially the same as P3P_3, because the two trajectories only swap the order of Q1,0=0Q_{1,0} = 0 and Q2,0=2Q_{2,0} = 2, and those two event types are unrelated.

Constraints

For 100%100\% of the testdata: N10N \leq 10, M15M \leq 15, Ci20C_i \leq 20, T106T \leq 10^6.

Translated by ChatGPT 5