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

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

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.

题解:

1.缩点:将颜色相同的连通块缩成一个点。当然并不是实际将其缩成一点,而是当前点与上一个点的颜色相同,则“不作为”, 即无视这个点。经过缩点之后,这棵树的结点颜色为黑白交替,如下图。

2.无环无向图的最长路:首先取随便一个点作为起点,然后dfs跑一遍。取得路径最长的那条路的终点,并以此作为起点,再跑一遍dfs,取最长的路径len,最终答案即为len/2,向下取整。

代码如下:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = 2e5+; int n, c[maxn];
vector<int>son[maxn]; pair<int,int> dfs(int u, int fa, int cnt)
{
pair<int,int> tmp = make_pair(cnt, u);
for(int i = ; i<son[u].size(); i++)
{
int v = son[u][i];
if(v==fa) continue; //由于无环,所以只要避免其走“回头路”即可,无需vis数组
tmp = max( tmp, dfs(v,u,cnt+(c[u]!=c[v])) ); //如果颜色相同,则cnt+0,就跳过了当前点
}
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);
son[u].push_back(v);
son[v].push_back(u);
} pair<int,int> tmp;
tmp = dfs(,-,);
tmp = dfs(tmp.second, -, );
cout<< tmp.first/ <<endl;
}

Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路的更多相关文章

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

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

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

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

  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. Truck History(最小生成树)

    poj——Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27703   Accepted: 10 ...

  2. 利用例子来理解spring的面向切面编程

    最近学习了spring的面向切面编程,在网上看到猴子偷桃的例子,觉得这种方式学习比书本上讲解有趣多了,也便于理解.现在就来基于猴子偷桃写个基本的例子. maven工程:

  3. 使用JAXP对xml文档进行DOM解析基础

    XML解析方式分为两种:dom和sax         dom:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种方式.       sax: ...

  4. mysql事务四种隔离级别

    事务的基本要素:原子性,一致性,隔离性,持久性. 事务并发问题:脏读,不可重复读,幻读. mysql隔离级别:read-uncommitted,read-committed,repeatable-re ...

  5. Mingw/Code::Block/wxWidgets 搭建

    Mingw The MinGW project maintains and distributes a number of different core components and suppleme ...

  6. Git以及github的使用方法(六),管理修改

    现在,假定你已经完全掌握了暂存区的概念.下面,我们要讨论的就是,为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件. 你会问,什么是修改?比如你新增了一行,这就是一个修改 ...

  7. Git以及github的使用方法(四),版本回退

    现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...

  8. class文件结构浅析(2)

    欢迎转载,转载需声明出处 ------------------ 请先看上一篇:Class类文件结构浅析 上一篇讲的都是理论.以下我们亲自实践一下. 首先编写一个简单的java类: public cla ...

  9. Google的分布式计算模型Map Reduce map函数将输入分割成key/value对

    http://www.nowamagic.net/librarys/veda/detail/1768 上一篇 大规模分布式数据处理平台Hadoop的介绍 中提到了Google的分布式计算模型Map R ...

  10. 2014年8月25日,收藏家和杀手——面向对象的C++和C(一)

    近期事情特别多,睡眠也都非常晚,有点精神和身体混乱的感觉,所以想写写技术分析文章.让两者的我都调整一下.这篇技术分析文章是一直想写的,当前仅仅是开篇,有感觉的时候就写写,属于拼凑而成,兴许的篇章没有时 ...