#P7418. [USACO21FEB] Counting Graphs P
[USACO21FEB] Counting Graphs P
Problem Description
Bessie has a connected undirected graph . has vertices numbered and edges (, ). may contain self-loops (an edge from a vertex to itself), but it does not contain multiple edges (multiple edges connecting the same pair of vertices).
Let be a boolean function. For each and , the function is true if there exists a path from vertex to vertex that uses exactly edges, and false otherwise. If an edge is traversed multiple times, it is counted that many times.
Elsie wants to copy Bessie. Specifically, she wants to construct an undirected graph such that for all and , we have .
Your task is to compute the number of graphs that Elsie can construct, modulo . Just like , may contain self-loops but cannot contain multiple edges (this means that for labeled vertices there are different graphs in total).
Each input contains () independent test cases. It is guaranteed that the sum of over all test cases does not exceed .
Input Format
The first line of input contains , the number of test cases.
The first line of each test case contains integers and .
Each of the next lines of the test case contains two integers and (), meaning that there is an edge connecting and in .
For readability, adjacent test cases are separated by a blank line.
Output Format
For each test case, output one line: the number of different graphs , modulo .
1
5 4
1 2
2 3
1 4
3 5
3
7
4 6
1 2
2 3
3 4
1 3
2 4
1 4
5 5
1 2
2 3
3 4
4 5
1 5
5 7
1 2
1 3
1 5
2 4
3 3
3 4
4 5
6 6
1 2
2 3
3 4
4 5
5 6
6 6
6 7
1 2
2 3
1 3
1 4
4 5
5 6
1 6
10 10
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
22 28
1 2
2 3
3 4
4 5
5 6
6 7
1 7
1 8
3 9
8 10
10 11
10 12
10 13
10 14
11 15
12 16
13 17
14 18
9 15
9 16
9 17
9 18
15 19
19 20
15 20
16 21
21 22
16 22
45
35
11
1
15
371842544
256838540
Hint
Explanation for Sample 1:
In the first test case, can be equal to , or one of the following two graphs:
5 4
1 2
1 4
3 4
3 5
5 5
1 2
2 3
1 4
3 4
3 5
Explanation for Sample 2:
There are some larger test cases. Make sure your answer is taken modulo . Note that the answer for the second-to-last test case is .
Test Point Properties:
- For an additional of the testdata, .
- For an additional of the testdata, .
- For an additional of the testdata, if it is not true that for all we have , then there exists such that is true and is false.
- For an additional of the testdata, there are no additional constraints.
Problem by: Benjamin Qi.
Translated by ChatGPT 5