Equivalence in Connectivity
Time limit2sMemory limit1024 MB
Given k graphs linked by single-edge additions or removals, group the graphs whose connected components are identical.
- Level
Hard9 of 10
- Topics
- Union-find, Graph, Segment tree, Divide and conquer
- Solved
- No attempts yet
Problem
Two undirected graphs of size are equivalent in connectivity if, for all , there is a path from to in one graph if and only if there is a path from to in the other graph.
You are given a sequence of graphs . Each graph has size . For each , there exists such that can be obtained from by adding or removing one edge. Divide the given graphs into groups so that two graphs are in the same group if and only if they are equivalent in connectivity.
Input
There are multiple test cases. The first line of input contains an integer (), the number of test cases. For each test case:
The first line contains three integers , , and (, ): the number of graphs, the number of vertices in each graph, and the number of edges in .
Each of the following lines contains two integers and (), denoting an edge of connecting and . It is guaranteed that has no multiple edges.
The -th of the following lines contains an integer , a string , and two integers and (, ). Each string is either add or remove.
If is add, then is obtained from by adding an edge connecting and . It is guaranteed that this edge does not exist in .
If is remove, then is obtained from by removing an edge connecting and . It is guaranteed that this edge exists in .
It is guaranteed that, over all test cases, the sum of , the sum of , and the sum of are each at most .
Output
For each test case, output the following.
On the first line, output an integer , the number of groups.
For each group, output one line containing the group size and the numbers of the graphs in the group. That is, output an integer followed by integers. The groups and the graphs within each group may be output in any order.