304409: CF843C. Upgrading Tree

Memory Limit:512 MB Time Limit:4 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Upgrading Tree

题意翻译

每次可以对一个树经行最多 $2n$ 次操作 $(x,y,y')$ 表示将 $(x,y)$ 删除,同时加入一条边 $(x,y')$ 。 每次操作要求 : 1. 每次操作中 $(x,y)$ 存在。 1. 操作后仍然是树。 1. 每次删了 $(x,y)$ 时,必须保证包含 $x$ 的子树中的节点数比包含 $y$ 的节点数大。 最后使得所有点距离的平方和最小。输出一种操作的方案。

题目描述

You are given a tree with $ n $ vertices and you are allowed to perform no more than $ 2n $ transformations on it. Transformation is defined by three vertices $ x,y,y' $ and consists of deleting edge $ (x,y) $ and adding edge $ (x,y') $ . Transformation $ x,y,y' $ could be performed if all the following conditions are satisfied: 1. There is an edge $ (x,y) $ in the current tree. 2. After the transformation the graph remains a tree. 3. After the deletion of edge $ (x,y) $ the tree would consist of two connected components. Let's denote the set of nodes in the component containing vertex $ x $ by $ V_{x} $ , and the set of nodes in the component containing vertex $ y $ by $ V_{y} $ . Then condition $ |V_{x}|>|V_{y}| $ should be satisfied, i.e. the size of the component with $ x $ should be strictly larger than the size of the component with $ y $ . You should minimize the sum of squared distances between all pairs of vertices in a tree, which you could get after no more than $ 2n $ 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 $ n $ ( $ 1<=n<=2·10^{5} $ ) — number of vertices in tree. The next $ n-1 $ lines of input contains integers $ a $ and $ b $ ( $ 1<=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 $ k $ ( $ 0<=k<=2n $ ) — the number of transformations from your example, minimizing sum of squared distances between all pairs of vertices. In each of the next $ k $ lines output three integers $ x,y,y' $ — indices of vertices from the corresponding transformation. Transformations with $ y=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. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF843C/5905cec6b2c0826d6ba38e3611ad2a2ba852ba57.png)

Input

题意翻译

每次可以对一个树经行最多 $2n$ 次操作 $(x,y,y')$ 表示将 $(x,y)$ 删除,同时加入一条边 $(x,y')$ 。 每次操作要求 : 1. 每次操作中 $(x,y)$ 存在。 1. 操作后仍然是树。 1. 每次删了 $(x,y)$ 时,必须保证包含 $x$ 的子树中的节点数比包含 $y$ 的节点数大。 最后使得所有点距离的平方和最小。输出一种操作的方案。

加入题单

上一题 下一题 算法标签: