Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径
3 seconds
256 megabytes
standard input
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.
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.
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.
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
2
4
0 0 0 0
1 2
2 3
3 4
0
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.
题意:你每次去一个点,可以将其同色的联通块一起变色,求最后变为一色的最少次数;
思路:将同色的看作一个点,每次去树的直径的中点即是最小次数;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e6+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
int clo[N];
struct is
{
int v,next;
}edge[N];
int head[N],edg;
int node1,ans;
void init()
{
edg=;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
edg++;
edge[edg].v=v;
edge[edg].next=head[u];
head[u]=edg;
}
void dfs(int u,int fa,int deep)
{
if(deep>ans)
{
ans=deep;
node1=u;
}
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
if(v==fa)continue;
if(clo[v]!=clo[u])
dfs(v,u,deep+);
else
dfs(v,u,deep);
}
}
int main()
{
init();
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&clo[i]);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(,-,);
dfs(node1,-,);
cout<<(ans+)/<<endl;
return ;
}
Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径的更多相关文章
- 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 ...
- 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
题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径
传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...
- 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 ...
随机推荐
- 设置Tab键为四个空格
https://my.oschina.net/xunxun10/blog/110074
- JSP注意点
一.JSP页面会编译成一个Servlet类,每个Servlet在容器中只有一个实例:在JSP中声明的变量是成员变量,成员变量只在创建实例时初始化,该变量的值将一直保存,直到实例销毁: 二.输出表达式& ...
- c#使用word、excel、pdf ——转
一.C# Word操作引入Word COM组件菜单=>项目=>添加引用=>COM=>Microsoft Word 11.0 Object Libraryusing Word = ...
- cocospod 安装和使用
一 ruby 安装 要安装coocspod 首先需要安装ruby,可以先安装xcode,在安装macport 下载地址,最后执行命令 port install ruby 二.安装CocoaPods 1 ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- destoon 深度整合discuz x2 UC 之免邮箱二次验证
destoon中member/my.php,信息发布入口处判断是否已在dx中做了验证,如果已经验证,则不再提示验证,否则其中dt的验证页面. 在home.php.php. group.php. for ...
- [BIM]案例
以下是中建三局BIM小组的项目,用以参考: BIM协同设计与质量控制 现实建筑物实体都是以三维空间状态存在,若用三维设计表达更具有优势.如复杂管综设计,一般情况下,二维AutoCAD设计是在建筑.结构 ...
- [ios][swift]swift GPS传感器的调用
在Info.plist文件中添加如下配置:(1)NSLocationAlwaysUsageDescription(2)NSLocationWhenInUseUsageDescription swift ...
- 在ubuntu上搭建turnserver
这边文章的目的:搭建turnserver,设定开机启动 1.下载turnserver的源码,最新的地址https://code.google.com/p/rfc5766-turn-server/ no ...
- A类地址
一个A类IP地址由1字节(每个字节是8位)的网络地址和3个字节主机地址组成,网络地址的最高位必须是“0”.A类IP的地址第一个字段范围是0~127,但是由于全0和全1的地址用作特殊用途,实际可指派的第 ...