#P6651. 「SWTR-5」Chain
「SWTR-5」Chain
Problem Description
You are given a directed acyclic graph (DAG) with vertices and edges. The graph is not guaranteed to be connected.
There are queries. In each query, you are given and pairwise distinct numbers . You need to compute: if these vertices are removed, how many chains will remain in the whole DAG. The answer should be taken modulo . Each query is independent.
-
Definition of a “chain”: Suppose a chain of length has the path . It should satisfy that has indegree and has outdegree . You can think of it as a food chain.
-
Two chains are “different” if and only if they have different lengths, or the sets of vertices they pass through are different.
-
Note carefully that chains newly created after deleting some vertices are not counted in the answer. For example, in the graph , there is chain . If vertex is removed, then chains remain.
Input Format
The first line contains two integers , representing the number of vertices and the number of edges.
The next lines each contain two integers , meaning there is a directed edge .
Line contains an integer , representing the number of queries.
The next lines:
- Each line first contains an integer , representing the number of vertices to remove.
- Then follow integers , representing the indices of the vertices to remove.
Output Format
Output lines. Each line is the answer for that query modulo .
7 14
3 2
4 5
2 5
2 6
3 1
3 5
3 7
3 6
6 4
1 4
6 5
1 6
7 2
7 4
7
3 2 4 6
2 4 6
2 2 5
2 1 4
0
1 4
1 6
1
3
0
6
13
7
5
233 1
1 2
6
0
1 10
2 10 40
1 1
1 2
2 1 2
232
231
230
231
231
231
Hint
"Sample 1 Explanation"
The DAG is shown in the figure:

Query : If are removed, then chain remains: .
Query : If are removed, then chains remain: , , .
Query : If is removed, then chains remain: , , , , .
"Constraints and Notes"
This problem uses bundled testdata.
- Subtask 1 (1 point): The given graph is a single chain.
- Subtask 2 (14 points): .
- Subtask 3 (20 points): .
- Subtask 4 (17 points): .
- Subtask 5 (18 points): .
- Subtask 6 (30 points): No special restrictions.
For of the testdata: , $1\leq m\leq \min(\frac{n\times(n-1)}{2},2\times 10^4)$, .
All queries satisfy: , , . It is guaranteed that all are pairwise distinct.
This problem is slightly time-tight, so please pay attention to IO optimization.
"Source"
Sweet Round 05 D.
Idea & solution: Alex_Wei.
Translated by ChatGPT 5