Simple Link Cut Problem

시간 제한1초메모리 제한1024 MB

요약
트리에 경로 회전 연산을 반복해 지름이 3 이하가 되도록 만들고, 사용한 연산 순서를 출력한다.
난이도

보통10점 중 7점

유형
트리, 그리디, 구현, 그래프
정답자
아직 제출이 없습니다

문제

You are given a tree with NN vertices. You can repeat the following operation at most 10510^5 times.

  • Choose four distinct vertices v_1,v_2,v_3,v_4v\_1, v\_2, v\_3, v\_4 such that there exist edges between v_1v\_1 and v_2v\_2, v_2v\_2 and v_3v\_3, v_3v\_3 and v_4v\_4. Remove these edges and add edges between v_1v\_1 and v_3v\_3, v_1v\_1 and v_4v\_4, v_2v\_2 and v_4v\_4.

Your task is transform the given tree so that its diameter is at most 33. Find a sequence of operations that does so.

입력

The first line contains one integer NN.

The ii-th of the following N−1N-1 lines contains space-separated two integers x_ix\_i and y_iy\_i, meaning that the ii-th edge connects vertices x_ix\_i and y_iy\_i in the tree.

출력

At the first line, print KK, the number of operations.

In the next KK line, print four integers v_1v\_1, v_2v\_2, v_3v\_3, v_4v\_4 separated by space.

If there are multiple solutions, print any. It can be proven that there exists at least one way to achieve the goal.

Note that you do not have to minimize KK.

제한

  • 4≤N≤1004 \leq N \leq 100
  • 1≤x_i,y_i≤N1 \le x\_i, y\_i \le N; x_i≠y_ix\_i \neq y\_i (1≤i≤N−1)(1 \le i \le N-1)
  • It is guaranteed that the given edges form a tree.
  • 0≤K≤100,0000 \leq K \leq 100,000
  • v_1,v_2,v_3,v_4v\_1, v\_2, v\_3, v\_4 should satisfy the conditions of the given operation.

힌트

The distance between two vertices uu and vv is defined as the number of the edges of the unique path from uu to vv.

The diameter of a tree is the maximum distance between any two vertices.

예제1

  1. 예제 1

    입력
    6
    1 2
    2 3
    3 4
    4 5
    5 6
    
    예상 출력
    3
    4 3 2 1
    6 5 4 1
    2 4 6 1