#P10725. [GESP202406 八级] 最远点对
[GESP202406 八级] 最远点对
Background
Related multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1156.
Problem Description
Xiao Yang has a tree with nodes. Each node in the tree is either white or black.
Xiao Yang wants to know the distance between the farthest pair of nodes that have different colors.
Input Format
The first line contains a positive integer , representing the number of nodes in the tree.
The second line contains non-negative integers (for all , equals or ). If , then node is white; if , then node is black.
Then there are lines. Each line contains two positive integers , meaning there is an edge connecting node and node .
It is guaranteed that in the input tree, there exist nodes with different colors.
Output Format
Output one integer, representing the distance between the farthest pair of nodes with different colors.
5
0 1 0 1 0
1 2
1 3
3 4
3 5
3
Hint
Sample Explanation
The farthest pair of nodes with different colors is node and node .
Constraints
This problem uses bundled testdata.
| Subtask ID | Score | Special Condition | ||
|---|---|---|---|---|
| The tree is a chain | ||||
For all testdata, it is guaranteed that and .
Translated by ChatGPT 5