2Trees

TimeLimit: Unknown ms  MemoryLimit:262144KB
64-bit integer IO format:%I64d
未提交 | 登录后收藏
Problem Description

Input/Output: standard input/output

You are given two trees with the same number of leaves L, your task is to merge the two trees' leaves in a way that ensures the following:

  • The number of colors needed to color the resulting graph such that no two adjacent nodes share the same color is minimum.
  • Each leaf in the first tree is merged into exactly one leaf from the second tree.
  • Each leaf in the second tree is merged into exactly one leaf from the first tree.
  • Nodes other than leaves are not merged.

Note that by merging two leaves a and b, the resulting node will have both edges of a and b.

Input

The first line of input contains one integer N (3 ≤ N ≤ 105), the number of nodes in the first tree.

Then follows N - 1 lines, the ith line contains two integers u and v (1 ≤ u, v ≤ N), the indices of the nodes connected by the ith edge in the first tree.

The next line contains an integer M (3 ≤ M ≤ 105), the number of nodes in the second tree.

Then follows M - 1 lines, the ith line contains two integers u and v (1 ≤ u, v ≤ M), the indices of the nodes connected by the ith edge in the second tree.

It is guaranteed that the two trees will have the same number of leaves L.

Output

On a single line print the number of colors needed to color the resulting graph.

Followed by L lines, the ith line of them contains two integers u and v (1 ≤ u ≤ N)(1 ≤ v ≤ M), the indices of the leaves to be merged, where u is a leaf in the first tree, and v is a leaf in the second tree.

If there’s more than one possible solution, print any of them.

SampleInput And SampleOutput
NULL
Submit
题目统计信息详细
总AC数2
通过人数2
尝试人数2
总提交量2
AC率100.00%
AC该题后可以添加标签
贴完标签可以获得20ACB。
并且可以获得本题所有提交代码查看权限。
点击标题可以显示标签。
如果你还没认真思考过这题,请不要查看标签
如果您已经通过了该题,请务为该题贴上标签

T^T Online Judge

[BUG反馈] [FAQ] [闽ICP备17026590号-1]
当前版本:3.24 系统时间: