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 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 缩点 直径的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径
传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree
题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...
- 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 ...
- 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 ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
- 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 ...
- 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 ...
随机推荐
- Entity Framework Code First 学习
1.添加entityframework 项目-管理解决方案的 NuGet 程序包-联机-Entity Framework 2.code first Migration 工具->库程序包管理器-& ...
- Java 第14章 字符串
字符串 基本数据类型和引用数据类型作为方法参数 ,在传递时有什么不同之处. 答:基本数据类型按值传递,相当于复制了一份过去. 引用数据类型是指向引用 内存地址,两个地方 根据地址使用同一份数据,如被更 ...
- vim/vi 复制,删除,粘贴,查找,替换
1.删除行 光标所在单行,dd 光标所在行以下的5行,5dd 全部删除:dG 2.复制 复制光标所在单行:yy 复制光标以下4行:4yy 3.粘贴 粘贴vi里复制的内容:p 粘贴外部复制过来的内容: ...
- 不可变String
1,什么是不可变String? String对象是不可变的.当试图修改String值的时候,实际上都是创建一个全新的String对象,该对象包含修改后字符串的值,而最初的对象则没有发生改变. pack ...
- React与ES6(三)ES6类和方法绑定
React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...
- Windows 8.1 应用再出发 - 视图状态的更新
本篇我们来了解一下Windows 8.1 给应用的视图状态带来了哪些变化,以及我们怎么利用这些变化作出更好的界面视图. 首先我们来简单回顾一下Windows 8.0 时代的视图状态: 上图中, ...
- 第38讲:List伴生对象操作方法代码实战
今天来看一下List伴生对像的操作方法 让我们来看下代码 println(List.apply(1,2,3))//等同于List(1,2,3) println(List.range(1, 4 ...
- Delphi 的字符及字符串[5] - 字符串与 Windows API
先说赋值: //赋值方法1: 给直接量 begin SetWindowText(Handle, '新标题'); end; //赋值方法2: 定义它要的类型 var p: PChar; begi ...
- [外挂4] 用CE查找棋盘基址
a.找棋盘数据基址 b.分析棋盘数据结构 综合使用搜索技巧,这要看你的聪明才智啦! [如本例:首先精确查找0,然后点一下左上角的一个,再次筛选出变化的,重开盘,再搜变化的,发现期盼规律为值为0表示没有 ...
- memcached与.NET的融合使用(二)
memcached部署完成之后,对当前缓存中数据的监控就显得比较迫切,这里看到网上开源的memadmin比较小巧好用,决定用它来查看监控memcached. 下载memadmin1.0.11,地址:h ...