#P7165. [COCI 2020/2021 #1] Papričice

    ID: 7984 远端评测题 1000ms 500MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>树形数据结构2020递归COCI(克罗地亚)

[COCI 2020/2021 #1] Papričice

Problem Description

Given a tree with nn nodes, the nodes are numbered from 11 to nn.

Now you need to cut (remove) two edges. This will split the tree into three connected components. Suppose the numbers of nodes in these three components are a,b,ca, b, c. Your task is to minimize max{a,b,c}min{a,b,c}\max\{a,b,c\}-\min\{a,b,c\}, and output this minimum value.

Input Format

The first line contains an integer nn, the number of nodes in the tree.
The next n1n-1 lines each contain two integers x,yx, y, representing an edge of the tree.

Output Format

Output one integer in a single line, representing the answer.

4
1 2
2 3
3 4

1
6
1 2
1 3
3 4
3 5
5 6
0
9
1 3
2 3
3 4
3 5
5 6
5 7
7 8
7 9
2

Hint

Sample 1 Explanation

An optimal solution can make the sizes of the three connected components 1,1,21,1,2, so the output is 21=12-1=1.

Sample 2 Explanation

Cut the edge between node 11 and node 33, and the edge between node 33 and node 55. The three resulting connected components have the same number of nodes.

Sample 3 Explanation

As shown in the figure below:

Constraints

This problem uses bundled testdata.

  • Subtask 1 (15 pts): 3n2003 \le n \le 200.
  • Subtask 2 (35 pts): 3n20003 \le n \le 2000.
  • Subtask 3 (60 pts): 3n2×1053 \le n \le 2 \times 10^5.

For 100%100\% of the testdata, 1x,yn1 \le x,y \le n.

The full score for this problem is 110110 points.

Notes

Translated from Croatian Open Competition in Informatics 2020 ~ 2021 Round 1 D Papričice

Translated by ChatGPT 5