Under construction

Still being built. Things may change

Equivalence in Connectivity

Time limit2sMemory limit1024 MB

Summary
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 nn are equivalent in connectivity if, for all 1≤u<v≤n1\le u<v\le n, there is a path from uu to vv in one graph if and only if there is a path from uu to vv in the other graph.

You are given a sequence of kk graphs G1,G2,…,GkG_1, G_2, \ldots, G_k. Each graph has size nn. For each i=2,3,…,ki=2,3,\ldots,k, there exists pi<ip_i<i such that GiG_i can be obtained from GpiG_{p_i} 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 TT (1≤T≤1051\le T\le 10^5), the number of test cases. For each test case:

The first line contains three integers kk, nn, and mm (1≤k,n≤1051 \le k, n \le 10^5, 0≤m≤min⁡(105,n(n−1)2)0 \le m \le \min(10^5, \frac{n(n-1)}{2})): the number of graphs, the number of vertices in each graph, and the number of edges in G1G_1.

Each of the following mm lines contains two integers uu and vv (1≤u<v≤n1\le u<v\le n), denoting an edge of G1G_1 connecting uu and vv. It is guaranteed that G1G_1 has no multiple edges.

The ii-th of the following k−1k-1 lines contains an integer pi+1p_{i+1}, a string ti+1t_{i+1}, and two integers xi+1x_{i+1} and yi+1y_{i+1} (1≤pi+1≤i1\le p_{i+1}\le i, 1≤xi+1<yi+1≤n1\le x_{i+1}<y_{i+1}\le n). Each string ti+1t_{i+1} is either add or remove.

If ti+1t_{i+1} is add, then Gi+1G_{i+1} is obtained from Gpi+1G_{p_{i+1}} by adding an edge connecting xi+1x_{i+1} and yi+1y_{i+1}. It is guaranteed that this edge does not exist in Gpi+1G_{p_{i+1}}.

If ti+1t_{i+1} is remove, then Gi+1G_{i+1} is obtained from Gpi+1G_{p_{i+1}} by removing an edge connecting xi+1x_{i+1} and yi+1y_{i+1}. It is guaranteed that this edge exists in Gpi+1G_{p_{i+1}}.

It is guaranteed that, over all test cases, the sum of nn, the sum of mm, and the sum of kk are each at most 10510^5.

Output

For each test case, output the following.

On the first line, output an integer rr, 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 ss followed by ss integers. The groups and the graphs within each group may be output in any order.

Examples1

  1. Example 1

    Input
    2
    15 11 8
    6 11
    1 6
    6 9
    6 8
    1 2
    1 5
    9 10
    2 5
    1 add 3 11
    1 add 2 3
    3 add 5 8
    4 add 5 11
    3 add 7 10
    1 add 6 10
    3 add 3 10
    1 remove 6 8
    5 add 4 9
    1 add 2 9
    8 add 7 8
    3 add 2 4
    1 remove 6 9
    10 remove 6 9
    14 5 2
    1 5
    1 4
    1 add 2 4
    1 add 3 4
    1 add 2 4
    4 add 3 4
    4 add 1 3
    5 add 1 3
    2 add 2 3
    1 add 1 2
    4 add 3 4
    3 add 4 5
    9 add 2 3
    3 remove 1 5
    3 remove 3 4
    
    Expected output
    7
    2 10 13
    5 2 3 4 5 8
    3 1 7 11
    1 14
    2 6 12
    1 9
    1 15
    5
    3 2 4 9
    6 5 6 7 8 10 12
    2 1 14
    2 3 11
    1 13