E. Anton and Tree

题目连接:

http://codeforces.com/contest/734/problem/E

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<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
int n,c[maxn];
vector<int> E[maxn];
pair<int,int> dfs(int x,int fa,int deep){
pair<int,int> tmp=make_pair(deep,x);
for(int i=0;i<E[x].size();i++){
int v = E[x][i];
if(v==fa)continue;
if(c[v]!=c[x])tmp=max(tmp,dfs(v,x,deep+1));
else tmp=max(tmp,dfs(v,x,deep));
}
return tmp;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&c[i]);
for(int i=1;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
E[a].push_back(b);
E[b].push_back(a);
}
pair<int,int> tmp=dfs(1,-1,0);
tmp=dfs(tmp.second,-1,0);
cout<<(tmp.first+1)/2<<endl;
}

Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径的更多相关文章

  1. 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 ...

  2. 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 ...

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

    传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...

  4. Codeforces Round #379 (Div. 2) E. Anton and Tree

    题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...

  5. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  6. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  7. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  8. Codeforces Round #379 (Div. 2) A. Anton and Danik 水题

    A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...

  9. Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟

    题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...

随机推荐

  1. Eclipse安装配置以及java项目和类的创建

    1.Eclipse的安装: 双击此应用程序 进入安装界面 选择下一步 更改路径将此默认路径改为 确定之后下一步更改jre的安装路径 在之前安装的java文件夹下新建一个jre文件夹 将jre安装在里边 ...

  2. JS-定时器换背景

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  3. Intellij IDEA 根据数据库自动生成pojo和hbm

    新建一个项目,每次写hibernate部分,就觉得pojo和hbm.xml部分很蛋疼.今天搜索了半天,终于知道如何根据数据库自动生成了. Intellij IDEA14创建maven时并不能勾选各种支 ...

  4. String的两种生成方式

    String的两种生成方式 第一种是双引号法,效率更高 java为String类提供了缓冲池机制,当使用双引号定义对象时,java环境首先去字符串缓冲池寻找相同内容的字符串,如果存在就直接拿出来应用, ...

  5. MySql自动分区

    自动分区需要开启MySql中的事件调度器,可以通过如下命令查看是否开启了调度器 show variables like '%scheduler%'; 如果没开启的话通过如下指令开启 ; 1.创建一个分 ...

  6. IT人士感言2(转)

    01. 自己的户口档案.养老保险.医疗保险.住房公积金一定要保管好.由于程序员行业每年跳槽一次,我不隐瞒大家,我至少换过5个以上的单位,这期间跳来跳去,甚至是城市都换过3个.还好户口没丢掉,其他都已经 ...

  7. Find Minimum in Rotated Sorted Array II leetcode

    题目链接 这个博客的算法思想简单好! 还是贴上自己的蹩脚代码吧!! class Solution { public: int findMin(vector<int>& nums) ...

  8. 并查集 Union-Find

    并查集能做什么? 1.连接两个对象; 2.查询两个对象是否在一个集合中,或者说两个对象是否是连接在一起的. 并查集有什么应用? 1. Percolation问题. 2. 无向图连通子图个数 3. 最近 ...

  9. Coins(HDU 2844):一个会超时的多重背包

    Coins  HDU 2844 不能用最基础的多重背包模板:会超时的!!! 之后看了二进制优化了的多重背包. 就是把多重转变成01背包: 具体思路见:http://www.cnblogs.com/tt ...

  10. [新概念51单片机C语言教程·郭天祥] 1、 基础知识必备

    目录: 单片机的大致介绍         1-1.通俗定义         1-2.51系列产品         1-3.标号意思         1-4.引脚介绍         1-5.用C语言开 ...