#P5865. [SEERC 2018] Tree
[SEERC 2018] Tree
Description
You are given a tree with vertices, numbered from to . Each vertex is colored either black or white. Select exactly black vertices so that the maximum distance among all pairs of selected black vertices is minimized. Output this minimal possible maximum distance.
Input Format
The first line contains two integers and , representing the number of vertices in the tree and the number of black vertices to be selected.
The second line contains integers . If , then vertex is black; otherwise it is white. The testdata guarantees that there are at least black vertices.
The next lines each contain two integers and , indicating that there is an edge between vertices and in the tree. The testdata guarantees that these edges form a tree.
Output Format
Output one integer, which is the answer.
6 3
1 1 0 1 1 1
1 2
1 3
1 4
3 5
3 6
2
9 4
1 0 1 0 1 0 0 1 1
1 2
2 4
2 3
4 5
1 6
6 7
6 8
7 9
5
Hint
In the first sample, the only possible choice is to select vertices , , and , and the maximum distance is .
In the second sample, one feasible choice is to select vertices , , , and . The maximum distance is the distance between vertices and .
Translated by ChatGPT 5
京公网安备 11011102002149号