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. java对xml文件做增删改查------摘录

    java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...

  2. JavaWeb学习总结(三)——Tomcat服务器学习和使用(二)

    一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:

  3. JQuery学习(层级)ancestor & descendant

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  4. mysql 授权

    cd /usr/local/mysql/bin/grant all privileges on *.* to 'root'@'%' identified by '12345678';flush pri ...

  5. 捉BUG记(To Catch a Bug)

    大约有一年整没有写一篇博客了,由于各种原(jia)因(ban)导致闲暇时间要么拿着IPad看岛国奇怪的片(dong)子(hua).要么拿着kindle看各种各样的资(xiao)料(shuo).本来想写 ...

  6. 【Android开发】 第一课 环境搭建教程

    Windows 开发环境部署: Android Studio 中文社区:http://www.android-studio.org/ 本教程将分为五个步骤来完成Android开发环境的部署. 第一步: ...

  7. JS备忘录

    /** *删除数组指定下标或指定对象 */ Array.prototype.remove = function (obj) { for (var i = 0; i < this.length; ...

  8. 将GitLab的数据库导入阿里云PostgreSQL RDS

    GitLab的数据库用的是PostgreSQL,之前由于阿里云RDS不支持PostgreSQL,只能将GitLab的数据库部署在云服务器上. 6月1日得知阿里云推出了PostgreSQL RDS,于是 ...

  9. Twisted网络编程入门

    Twisted是用Python实现的基于事件驱动的网络引擎框架,功能非常丰富,基本包括了常用的网络组件. 所谓事件驱动,就是说程序就像是一个报警器(reactor),时刻等待着外部事件(event), ...

  10. Visual Studio 发布新版API智能提示

    Visual Studio 新版API智能提示两周前发布.有了它,你可以在调用API的同时,方便了解到API的相关示例代码.这大大地有助于开发人员学习和使用API. 安装方法如下: 1. 打开Visu ...