博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
阅读量:6682 次
发布时间:2019-06-25

本文共 2125 字,大约阅读时间需要 7 分钟。

E. Anton and Tree

题目连接:

Description

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 and u). 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.

Input

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.

Output

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.

Sample Input

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

Sample Output

2

Hint

题意

给你一棵树,每个树上的节点要么是1,要么是0

每次操作你可以指定任何一个节点,然后使得这个节点周围的同色连通块变色。

问你最少花费多少次,使得整个树都是一个颜色。

题解:

显然缩点,缩点之后,我们把直径找出来,显然答案就是直径+1除以2

因为你在缩直径的时候,会把枝叶顺便给缩了,每次会减小2的长度。

代码

#include
using namespace std;const int maxn = 2e5+7;int n,c[maxn];vector
E[maxn];pair
dfs(int x,int fa,int deep){ pair
tmp=make_pair(deep,x); for(int i=0;i
tmp=dfs(1,-1,0); tmp=dfs(tmp.second,-1,0); cout<<(tmp.first+1)/2<

转载地址:http://dvxao.baihongyu.com/

你可能感兴趣的文章
rpm 与 yum 用法
查看>>
Oracle数据库新版本12c信息汇总
查看>>
【Oracle Database 12c新特性】 In-Database Archiving数据库内归档
查看>>
运维自动化之psutil模块
查看>>
mysql的复制原理以及复制类型
查看>>
iOS开发系统类功能划分
查看>>
知道IP地址的情况下,如何查看主机名
查看>>
SQL执行过程
查看>>
Puppet批量部署tomcat
查看>>
OSPF与Loopback地址
查看>>
mysql的sql执行计划详解
查看>>
Cacti 0.8.8b 配置spine
查看>>
ppt免费模板下载
查看>>
有关字符串中的函数及其部分面试题
查看>>
试用分区助手心得
查看>>
IP地址被恶意域名解析
查看>>
完美安装centos7编译安装php5.6.40(亲测成功!)
查看>>
CentOS7基于虚拟用户的vsfptd
查看>>
[1]supervisor的使用管理:实现对异常中断的子进程的自动重启(以tomcat为例)
查看>>
关于配置Radius认证服务器的思路与配置方法
查看>>