题目名称 2624. Anton and Tree
输入输出 AntonandTree.in/out
难度等级 ★★★☆
时间限制 1000 ms (1 s)
内存限制 256 MiB
测试数据 10
题目来源 Marvolo 于2017-03-03加入修改该题
开放分组 全部用户
提交状态
分类标签
分享题解
通过:8, 提交:18, 通过率:44.44%
张浩南 100 0.163 s 8.12 MiB C++
房子涵 100 0.206 s 5.63 MiB C++
KKK 100 0.210 s 13.55 MiB C++
Marvolo 100 0.216 s 8.71 MiB C++
Marvolo 100 0.221 s 8.71 MiB C++
李紫临 100 0.443 s 28.92 MiB C++
Jerry 100 0.497 s 10.79 MiB C++
Jerry 100 0.535 s 11.55 MiB C++
KKK 90 0.189 s 6.39 MiB C++
真实核糖核酸 80 0.283 s 3.36 MiB C++
关于 Anton and Tree 的近10条评论(全部评论)
dfs的话记得手工栈或者开栈
FoolMike
2017-03-07 13:55 3楼
回复 @sherc : srO Sherc Orz
Marvolo
2017-03-04 13:13 2楼
我有罪,拉低了正确率。。。。。
再见
2017-03-03 21:40 1楼

2624. Anton and Tree

★★★☆   输入文件:AntonandTree.in   输出文件:AntonandTree.out   简单对比
时间限制:1 s   内存限制:256 MiB

【题目描述】


Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph.

There are n vertices in the tree, each of them is painted black or white. Anton doesn't like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white).

To change the colors Anton can use only operations of one type. We denote it as paint(v), where v is some vertex of the tree. This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v andu). For example, consider the tree



and apply operation paint(3) to get the following:



Anton is interested in the minimum number of operation he needs to perform in order to make the colors of all vertices equal.


【输入格式】


The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.

The second line contains n integers colori (0 ≤ colori ≤ 1) — colors of the vertices. colori = 0 means that the i-th vertex is initially painted white, while colori = 1 means it's initially painted black.

Then follow n - 1 line, each of them contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — indices of vertices connected by the corresponding edge. It's guaranteed that all pairs (ui, vi) are distinct, i.e. there are no multiple edges.


【输出格式】

Print one integer — the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white.

【样例输入】

11

0 0 0 1 1 0 1 0 0 1 1

1 2

1 3

2 4

2 5

5 6

5 7

3 8

3 9

3 10

9 11

【样例输出】

2

【来源】

CF #379 Div2 E