luogu#P4489. [CTSC2009] 纷繁世界
[CTSC2009] 纷繁世界
Background
This is a complex and intricate world.
One morning you woke up late and missed breakfast. You rode your bike to the supermarket, only to find that the staff had already put up new price tags and all snacks had gone up by . In anger, you stayed hungry the whole morning.
Of course, things might not have turned out that way.
Another morning you woke up a bit late and still missed breakfast. You rode your bike to the supermarket, and it happened that the staff had not yet put up the new price tags. You smoothly bought some breakfast and went on your way.
Perhaps you would get up earlier, or perhaps the store staff would arrive late.
Sometimes people complete tasks in an expected order, but for some events, the relative order in which they occur can influence how the world develops.
For example, if the store has only one watermelon, and both you and I want to buy it, then the order in which we try to buy it will directly determine who gets it.
Analyzing the rules of operation in such a complicated world is very important, and that is exactly what you are going to study.
Problem Description
For simplicity, assume there are people and 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 is related to event , then event is also related to event .
Let be the sequence of events person plans to complete (called the plan sequence), where is the length of . Each event is one of the 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. is an ordered sequence satisfying:
- For every person, each event in their plan sequence appears in exactly once.
- For any two events and belonging to the same plan sequence (), must occur before (i.e., appear earlier in ).
Two trajectories and are defined to be essentially different if and only if there exist two related events and whose relative order differs in and . In other words, if in the event occurs before while in the event occurs after , then and are essentially different; similarly, if in occurs after but in it occurs before , then and are also essentially different.
Note: “Essentially the same” is transitive. That is, if is essentially the same as , and is essentially the same as , then is necessarily essentially the same as .
Given , , 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 .
- The second line contains an integer denoting the number of event types . All event types are numbered from to .
- Then, for each person , the description of their plan sequence is given:
- The first line contains one integer, the sequence length .
- The second line contains integers, giving each event in in order.
- Finally, lines follow, containing an matrix dep that describes the relatedness. Each line contains integers, each being or . dep(i, j) denotes the integer in the -th row (from top to bottom) and the -th column (from left to right). If dep(i, j) equals , then event type and event type are related; otherwise, they are unrelated.
Output Format
Output a single line containing one integer, the number 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 people and event types, with . , , , .
There are different development trajectories:
- ;
- ;
- ;
- .
Any other valid development trajectory is necessarily essentially the same as one of these four. For example, is essentially the same as , because the two trajectories only swap the order of and , and those two event types are unrelated.
Constraints
For of the testdata: , , , .
Translated by ChatGPT 5