E. Anton and Tree
time limit per test:

3 seconds

memory limit per test

:256 megabytes

input:standard input
output:

standard output

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.

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.

Examples
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
output
2
input
4
0 0 0 0
1 2
2 3
3 4
output
0
Note

In the first sample, the tree is the same as on the picture. If we first apply operation paint(3) and then apply paint(6), the tree will become completely black, so the answer is 2.

In the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0.

题目链接:http://codeforces.com/contest/734/problem/E


题意:给你一棵黑白树,每次paint(v)操作可以指定任何一个节点v,然后使得这个节点周围的同色连通块变色。问你最少花费多少次,使得整个树都是一个颜色。

思路:因为是同色联通块一起变色,所以把同色联通块缩成一个点。那个就成了一棵黑白相间的树。最少只需要把这棵树的直径(长度为L)的中点变换L/2次。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+;
int n,c[maxn];
vector<int> G[maxn];
pair<int,int> dfs(int u,int fa,int deep)
{
pair<int,int> tmp=make_pair(deep,u);
for(int i=; i<G[u].size(); i++)
{
int v=G[u][i];
if(v==fa)continue;
if(c[v]!=c[u]) tmp=max(tmp,dfs(v,u,deep+));
else tmp=max(tmp,dfs(v,u,deep));
}
return tmp;
}
int main()
{
scanf("%d",&n);
for(int i=; i<=n; i++)scanf("%d",&c[i]);
for(int i=; i<n; i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
pair<int,int> tmp=dfs(,-,);
tmp=dfs(tmp.second,-,);
cout<<tmp.first/<<endl;
}

DFS

Codeforces 734E. Anton and Tree 搜索的更多相关文章

  1. Codeforces 734E Anton and Tree(缩点+树的直径)

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

  2. CodeForces 734E Anton and Tree

    $dfs$缩点,树形$dp$. 首先将连通块缩点,缩点后形成一个黑白节点相间的树.接下来的任务就是寻找一个$root$,使这棵树以$root$为根,树的高度是最小的(也就是一层一层染色).树形$dp$ ...

  3. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  4. Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路

    题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...

  5. Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径

    E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...

  6. Anton and Tree

    Anton and Tree 题目链接:http://codeforces.com/contest/734/problem/E DFS/BFS 每一次操作都可以使连通的结点变色,所以可以将连通的点缩成 ...

  7. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  8. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  9. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

随机推荐

  1. 欧冠杯:葡萄牙VS法国——葡萄牙首次夺冠!

    北京时间7月11日凌晨3点,2016欧洲杯决赛,葡萄牙在法兰西大球场1-0击败东道主法国,历史上首次夺冠.C罗伤退落泪,替补出场的埃德尔在加时赛第109分钟完成绝杀. 昨晚上本来准备熬夜看球赛的!而且 ...

  2. 答:SQLServer DBA 三十问之三:有哪些操作会使用到TempDB;如果TempDB异常变大,可能的原因是什么,该如何处理

    3. 有哪些操作会使用到TempDB:如果TempDB异常变大,可能的原因是什么,该如何处理: tempdb的用途:1)存储专用和全局临时变量,不考虑数据库上下文: 2)与Order by 子句,游标 ...

  3. 【转载】[C#]Log4net中的RollingFileAppender解析

    Log4日志组件的应用确实简单实用,在比较了企业库和Log4的日志功能后,个人觉得Log4的功能更加强大点.补充说明下,我使用的企业库是2.0版本,Log4net是1.2.1版本的. 在Log4net ...

  4. C# MVC ( 添加路由规则以及路由的反射机制 )

    在项目文件夹下找到 App_Start 下 找到 RouteConfig.cs文件 打开   (1) 约束的规则 从上往下 贪婪性   (2) 用 routes.MapRoute(...) 添加   ...

  5. windows程序里新窗体不在任务栏显示,无标题拖动,键盘事件,始终显示在主窗体上面,单实例运行等

    不显示在任务栏,Alt+Tab也不显示 protected override CreateParams CreateParams { get { const int WS_EX_APPWINDOW = ...

  6. linux 学习 12 服务管理

      第十二讲 Linux服务管理 12.1 Linux服务管理-服务分类 ——Linux服务 ----RPM包默认安装的服务 ————独立的服务 ————基于xinetd服务 ----源码包安装的服务 ...

  7. ECshop 数据库表结构

    -- 表的结构 `ecs_account_log`CREATE TABLE IF NOT EXISTS `ecs_account_log` (`log_id` mediumint(8) unsigne ...

  8. vc++ 程序开机自启动和取消启动

    //开机启动 int CMainWnd::CreateRun() { //添加以下代码 HKEY hKey; }; //得到程序自身的全路径 DWORD dwRet = GetModuleFileNa ...

  9. linux下安装kears

    2. 安装python基础开发包 # 系统升级 sudo apt update sudo apt upgrade sudo apt install -y python-dev python-pip p ...

  10. c++ 二维数组传递

    c++ 二维数组传递 我们在传递二维数组时,对于新手来说,可能会存在某些问题,下面讲解几种传递方法 在讲解如何传递二维数组时,先看看如何动态new 二维数组 // 二维数组动态申请 int row , ...