CF843C.Upgrading Tree

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree with nn vertices and you are allowed to perform no more than 2n2n transformations on it. Transformation is defined by three vertices x,y,yx,y,y' and consists of deleting edge (x,y)(x,y) and adding edge (x,y)(x,y') . Transformation x,y,yx,y,y' could be performed if all the following conditions are satisfied:

  1. There is an edge (x,y)(x,y) in the current tree.
  2. After the transformation the graph remains a tree.
  3. After the deletion of edge (x,y)(x,y) the tree would consist of two connected components. Let's denote the set of nodes in the component containing vertex xx by VxV_{x} , and the set of nodes in the component containing vertex yy by VyV_{y} . Then condition |V_{x}|>|V_{y}| should be satisfied, i.e. the size of the component with xx should be strictly larger than the size of the component with yy .

You should minimize the sum of squared distances between all pairs of vertices in a tree, which you could get after no more than 2n2n transformations and output any sequence of transformations leading initial tree to such state.

Note that you don't need to minimize the number of operations. It is necessary to minimize only the sum of the squared distances.

输入格式

The first line of input contains integer nn ( 1<=n<=21051<=n<=2·10^{5} ) — number of vertices in tree.

The next n1n-1 lines of input contains integers aa and bb ( 1<=a,b<=n,ab1<=a,b<=n,a≠b ) — the descriptions of edges. It is guaranteed that the given edges form a tree.

输出格式

In the first line output integer kk ( 0<=k<=2n0<=k<=2n ) — the number of transformations from your example, minimizing sum of squared distances between all pairs of vertices.

In each of the next kk lines output three integers x,y,yx,y,y' — indices of vertices from the corresponding transformation.

Transformations with y=yy=y' are allowed (even though they don't change tree) if transformation conditions are satisfied.

If there are several possible answers, print any of them.

输入输出样例

  • 输入#1

    3
    3 2
    1 3
    

    输出#1

    0
    
  • 输入#2

    7
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
    

    输出#2

    2
    4 3 2
    4 5 6

说明/提示

This is a picture for the second sample. Added edges are dark, deleted edges are dotted.

首页