#P15416. 「yrOI R1」夏日已逝

    ID: 17337 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>洛谷原创Special JudgeO2优化树链剖分构造

「yrOI R1」夏日已逝

Background

Problem Description

You are given two trees S,TS, T of size nn. Define one operation on SS as follows:

  • Choose any diameter (p,q)(p, q) of SS.
  • Choose any node uu on this diameter, then choose a node vv adjacent to uu that is not on this diameter, then choose another node ww on this diameter, and perform:
  • Delete the edge (u,v)(u, v), and add the edge (v,w)(v, w).

You need to determine whether it is possible to apply the operation any number of times to transform SS into some S′S', such that S′S' is isomorphic to TT. If it is possible, you need to output one valid plan within 4n4n operations.

Input Format

The first line contains a positive integer nn, which is the size of trees SS and TT.

The next n−1n - 1 lines each contain two integers (ai,bi)(a_i, b_i), representing an edge of tree SS.

The next n−1n - 1 lines each contain two integers (ci,di)(c_i, d_i), representing an edge of tree TT.

Output Format

Output a string Yes\texttt{Yes} or No\texttt{No} on the first line, indicating whether it is possible to make SS isomorphic to TT using the operations.

If you output Yes\texttt{Yes}, then on the next line output an integer kk, the number of operations in your constructed plan.

You must ensure that the number of operations is at most 4n4n. If your number of operations exceeds 4n4n, you will be judged as Wrong Answer.

In the next kk lines, output five integers (pi,qi,ui,vi,wi)(p_i, q_i, u_i, v_i, w_i), describing one operation.

Then you need to output one line pip_i, meaning that in the tree SS after all operations, node xx corresponds to node pxp_x in tree TT.

This problem uses a Special Judge. If you correctly output Yes\texttt{Yes} or No\texttt{No}, you will get 20%20\% of the score for that subtask. Note: if you output Yes\texttt{Yes}, you must output a plan afterwards (even if it may be invalid; you can directly output n+1n + 1 zeros to do this).

7
1 2
2 3
3 7
3 4
4 5
5 6
7 6
6 5
5 4
4 3
3 2
3 1
Yes
1
1 6 3 7 5
7 6 5 4 3 2 1
4
1 2
2 3
3 4
1 2
1 3
1 4
No

Hint

This problem uses bundled testdata.

  • Subtask 1 (5 pts): n≤10n \le 10.
  • Subtask 2 (5 pts): SS is a path.
  • Subtask 3 (5 pts): TT is a path.
  • Subtask 4 (5 pts): SS is a star (also called "juhua", 菊花).
  • Subtask 5 (10 pts): SS and TT each have only one diameter, and all nodes with degree >2> 2 exist only on the diameter.
  • Subtask 6 (10 pts): The initial diameters of SS and TT have the same length.
  • Subtask 7 (25 pts): n≤500n \le 500.
  • Subtask 8 (35 pts): No special constraints.

For 100%100\% of the data, 1≤ai,bi,ci,di≤n≤2×1031 \le a_i, b_i, c_i, d_i \le n \le 2 \times 10^3.


When all journeys end, and I fall into sleep.

I will surely return to this summer.

Translated by ChatGPT 5