题目链接: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. 推荐一款基于 AI 开发的 IDE 插件,帮助提升编码效率

    最近在浏览技术社区的时候,发现了一款神奇 IDE 插件,官网称可以利用 AI 帮助程序员写代码,一下子吸引了我的好奇心.赶紧下载下来使用一番,感觉确实蛮神奇,可以火速提升编程效率. 这款插件叫做 ai ...

  2. Maven 项目打包发布

    在Eclipse左侧右击项目,Debug As -> Debug Configurations -> 双击Maven Build 然后看弹出框的右侧右侧的Name随便填写一个名字,Base ...

  3. es6系列-变量的解构赋值

    git地址: https://github.com/rainnaZR/es6-study/tree/master/src/destructuring 变量的解构赋值 变量的解构赋值: 数组, 对象, ...

  4. 【java】httpclient的使用之java代码内发送http请求

    场景: 需要本项目发送HTTP请求到另一个项目中,处理完成返回值给本项目. 1.本项目引入架包 <!-- httpclient 后台发送http请求--> <dependency&g ...

  5. 浮窗WindowManager view返回和Home按键事件监听

    出于功能需求,需要在所有的view之上显示浮窗,于是需要在WindowManager的View上处理返回键的响应, mFloatingWindowView = layoutInflater.infla ...

  6. Android:MVC模式(下)

    在上一篇文章中,我们将 View 类单独出来并完成了设计和编写.这次我们将完成 Model 类,并通过 Controller 将两者连接起来,完成这个计算器程序. 模型(Model)就是程序中封装了数 ...

  7. flask的restful api模块flask_restful和认证模块flask_httpauth

    参考: 1.https://zhuanlan.zhihu.com/p/24629177 2.https://github.com/shengulong/LearnPython/blob/master/ ...

  8. EasyHook库系列使用教程之四钩子的启动与停止

    此文的产生花费了大量时间对EasyHook进行深入了解同一时候參考了大量文档 先来简单比較一下EasyHook与Detour钩取后程序流程 Detours:钩取API函数后.产生两个地址,一个地址相应 ...

  9. POJ 1017 Packets(积累)

    [题意简述]:这个是别人的博客,有清晰的题意描写叙述.和解题思路,借助他的想法,能够非常好的解决问题! [分析]:贪心?模拟?见代码 //216K 16Ms #include<iostream& ...

  10. myBatis-plus异常提示For input string: "{0=null}"

    异常信息 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Per ...