#B3904. Graph Transformation Sequence

    ID: 11561 Type: Default 1000ms 256MiB

Graph Transformation Sequence

Graph Transformation Sequence

We are given two undirected simple graphs with the same vertex set: an initial graph SS and a target graph TT. The target graph is assumed to be constructed by the following transformation procedure from SS:

  1. Initialize an empty graph TT on nn vertices (i.e. no edges).
  2. While there are at least two vertices in SS, choose two vertices uu and vv from SS that have equal degree (i.e. $$deg_S(u)=deg_S(v)$$). Then, add the edge (u,v)(u,v) to TT and remove vertex uu (together with all edges incident to uu) from SS.
  3. When only one vertex remains in SS, the constructed graph TT is obtained.

Moreover, one may iterate this procedure: starting from SS, we perform one transformation ST1S\to T_1, from which a further transformation T1T2T_1\to T_2 (and so on) may be done. In this problem, you are given SS and TT (both on nn vertices) and your task is to find at least one sequence of transformations (with at least 11 and at most 33 transformation steps) such that

ST1T2Tk=T,S\to T_1\to T_2\to \cdots \to T_k=T,

where in each transformation the chosen edges (the pairs (u,v)(u,v) selected in that procedure) exactly form the constructed tree. The input is guaranteed to have a solution. If there are multiple valid answers, output any one of them.

inputFormat

The first line contains a single integer nn (the number of vertices). Then follows the description of the graph SS: • An integer m1m_1 representing the number of edges in SS, followed by m1m_1 lines each containing two integers uu and vv (1-based) that denote an edge between vertices uu and vv. Next is the description of the graph TT: • An integer m2m_2 representing the number of edges in TT, followed by m2m_2 lines each containing two integers uu and vv that denote an edge between vertices uu and vv.

outputFormat

Output a valid transformation sequence which consists of exactly kk transformation procedures (with 1k31\le k\le 3). For each transformation procedure, output an integer rr on a separate line (which should equal n1n-1, the number of operations in that procedure), followed by rr lines each containing two integers uu and vv representing the pair chosen in that operation. The overall first line of the output should be the integer kk. (Note: In our solutions we use k=1k=1, i.e. a single transformation.)

sample

3
3
1 2
1 3
2 3
2
1 2
2 3
1

2 1 2 3 2

</p>