310669: CF1867F. Most Different Tree

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

Description

F. Most Different Treetime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Given a tree with $n$ vertices rooted at vertex $1$, denote it as $G$. Also denote $P(G)$ as the multiset of subtrees of all vertices in tree $G$. You need to find a tree $G'$ of size $n$ rooted at vertex $1$ such that the number of subtrees in $P(G')$ that are isomorphic to any subtree in $P(G)$ is minimized.

A subtree of vertex $v$ is a graph that contains all vertices for which vertex $v$ lies on the path from the root of the tree to itself, as well as all edges between these vertices.

Two rooted trees are considered isomorphic if it is possible to relabel the vertices of one of them so that it becomes equal to the other, with the root of the first tree receiving the number of the root of the second tree.

Input

The first line contains a single integer $n$ ($2 \le n \le 10^6$) - the number of vertices in tree $G$. Each of the next $n-1$ lines contains two integers $a$ and $b$ $(1 \leq a,b \leq n)$, indicating that there is an edge between vertices $a$ and $b$ in the tree.

Output

Output $n-1$ lines, each line containing two numbers $a$, $b$ $(1 \leq a,b \leq n)$ - the edges of tree $G'$. If there are multiple optimal answers, output any.

ExamplesInput
2
1 2
Output
1 2
Input
3
1 2
1 3
Output
1 2
2 3
Input
4
1 2
1 3
3 4
Output
1 2
2 3
3 4

Output

题目大意:
给定一个以顶点1为根的n个顶点的树G。P(G)表示树G中所有顶点的子树的集合。需要找到一个以顶点1为根的n个顶点的树G',使得P(G')中与P(G)中任何子树同构的子树数量最小化。

一个顶点v的子树是一个图,它包含所有顶点,使得顶点v位于从树根到自身的路径上,以及这些顶点之间的所有边。

如果可以通过重新标记一个树的顶点使其与另一个树相等,并且第一个树的根顶点获得第二个树的根顶点的编号,则认为这两个有根树是同构的。

输入数据格式:
第一行包含一个整数n(2≤n≤10^6),表示树G的顶点数。接下来的n-1行,每行包含两个整数a和b(1≤a,b≤n),表示树G中顶点a和顶点b之间有一条边。

输出数据格式:
输出n-1行,每行包含两个数a,b(1≤a,b≤n),表示树G'的边。如果有多个最优解,输出其中任意一个。

示例输入输出:
输入:
2
1 2
输出:
1 2

输入:
3
1 2
1 3
输出:
1 2
2 3

输入:
4
1 2
1 3
3 4
输出:
1 2
2 3
3 4题目大意: 给定一个以顶点1为根的n个顶点的树G。P(G)表示树G中所有顶点的子树的集合。需要找到一个以顶点1为根的n个顶点的树G',使得P(G')中与P(G)中任何子树同构的子树数量最小化。 一个顶点v的子树是一个图,它包含所有顶点,使得顶点v位于从树根到自身的路径上,以及这些顶点之间的所有边。 如果可以通过重新标记一个树的顶点使其与另一个树相等,并且第一个树的根顶点获得第二个树的根顶点的编号,则认为这两个有根树是同构的。 输入数据格式: 第一行包含一个整数n(2≤n≤10^6),表示树G的顶点数。接下来的n-1行,每行包含两个整数a和b(1≤a,b≤n),表示树G中顶点a和顶点b之间有一条边。 输出数据格式: 输出n-1行,每行包含两个数a,b(1≤a,b≤n),表示树G'的边。如果有多个最优解,输出其中任意一个。 示例输入输出: 输入: 2 1 2 输出: 1 2 输入: 3 1 2 1 3 输出: 1 2 2 3 输入: 4 1 2 1 3 3 4 输出: 1 2 2 3 3 4

加入题单

上一题 下一题 算法标签: