#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 nn 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 nn, representing the number of nodes in the tree.

The second line contains nn non-negative integers a1,a2,,ana_1,a_2,\cdots,a_n (for all 1in1\le i\le n, aia_i equals 00 or 11). If ai=0a_i=0, then node ii is white; if ai=1a_i=1, then node ii is black.

Then there are (n1)(n-1) lines. Each line contains two positive integers xi,yix_i,y_i, meaning there is an edge connecting node xix_i and node yiy_i.

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 22 and node 55.

Constraints

This problem uses bundled testdata.

Subtask ID Score nn aia_i Special Condition
11 3030 105\le 10^5 0ai10\le a_i\le 1 The tree is a chain
22 103\le 10^3
33 4040 105\le 10^5

For all testdata, it is guaranteed that 1n1051\le n\le 10^5 and 0ai10\le a_i\le 1.

Translated by ChatGPT 5