#P2092. Tree Transform

Tree Transform

题目描述

点击这里查看pdf

MianKing has two labeled unrooted trees SS,TT with nn nodes and he wants to transform SS to TT by some operations.

In each operation, MianKing can select four distinct nodes x,y,z,wx,y,z,w which forms a path (x,y),(y,z),(z,w)\\\\{(x,y),(y,z),(z,w)\\\\} in the tree SS. Then he removes edges (x,y),(y,z),(z,w)\\\\{(x,y),(y,z),(z,w)\\\\} from SS , chooses three new edges whose endpoints inx,y,z,w\\in \\\\{x,y,z,w\\\\} and adds these new edges to SS. MianKing has to guarantee that SS is still a tree after this operation.

Now you need to help MianKing to transform SS to TT within 10410^4 operations.

输入格式

The first line has one integer nn.

Then there are n1n-1 lines, each lines has two integers (x,y)(x,y) which denotes an edge in SS.

Then there are n1n-1 lines, each lines has two integers (x,y)(x,y) which denotes an edge in TT.

4leqnleq1004\\leq n\\leq 100

It's guaranteed that SS and TT are both trees.

输出格式

The first line has one string "YES" if there exists a solution to transform SS to TT within 10410^4 operations, otherwise "NO". (both without quotation)

If there exists a solution, then:

The first line has one integers mm which denotes the number of operations of your solution.

Then for each operations, there are two lines which represent this operations. The first line has four integers (x,y,z,w)(x,y,z,w) and the second line has six integers a1,b1,a2,b2,a3,b3a_1,b_1,a_2,b_2,a_3,b_3 which (ai,bi)(a_i,b_i) denotes the i-th new edge you choose in this operation.

For each operation, you should guarantee these conditions:

  1. 1leqx,y,z,w,ai,bileqn1\\leq x,y,z,w,a_i,b_i\\leq n

  2. x,y,z,wx,y,z,w are distinct and form a path (x,y),(y,z),(z,w)\\\\{(x,y),(y,z),(z,w)\\\\} in the tree SS at that time.

  3. ai,biinx,y,z,wa_i,b_i \\in \\\\{x,y,z,w\\\\}

  4. After removing edges (x,y),(y,z),(z,w)\\\\{(x,y),(y,z),(z,w)\\\\} and add new edges (a1,b1),(a2,b2),(a3,b3)(a_1,b_1),(a_2,b_2),(a_3,b_3), SS is still a tree at that time.

  5. After all of the operations, SS should be the same as TT.

  6. 0leqmleq1040\\leq m\\leq 10^4

Two labeled tree are same if and only if [(x,y)inS]leftrightarrow[(x,y)inT][(x,y)\\in S] \\leftrightarrow [(x,y)\\in T] for all edge (x,y)(x,y)

样例

5 
1 2 
2 3 
3 4 
2 5 
1 2 
2 3 
3 4 
1 5 

YES 
2 
5 2 3 4 
2 3 3 5 3 4 
1 2 3 5 
1 5 1 2 2 3
4 
1 2 
1 3 
1 4 
1 2 
2 3 
3 4 

NO 

4 
1 2 
1 3 
1 4 
1 2 
1 3 
1 4 

YES 
0 

提示

Form 2020ICPC济南站