Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径
题意:
这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色。问最少操作次数,使得树上颜色相同。
思路:
先缩点,把相同的颜色的相邻节点缩在一起。再求出树的最长直径S(边的个数),答案就是(S + 1)/ 2;
因为对于一条链,我们可以从中间向两边交换改变。
#include <bits/stdc++.h>
#define pb push_back
using namespace std; const int MAXN = 2e5+;
vector<int>mp[MAXN],newmap[MAXN];
int n,a[MAXN],col[MAXN],h[MAXN],fa[MAXN];
bool vis[MAXN];
int ans = ;
void dfs(int u, int c){
vis[u] = true;
col[u] = c;
for(int i=; i < mp[u].size(); i++){
int v = mp[u][i];
if(a[v]!=a[u])continue;
if(vis[v])continue;
dfs(v,c);
}
}
void dfs2(int u, int o){ int mx = ,mxx = ;
for(int i=; i<newmap[u].size(); i++){
int v = newmap[u][i];
if(v==o)continue;
dfs2(v,u);
mx = max(mx,h[v] + );
if(mx > mxx)swap(mx,mxx);
}
h[u] = mxx;
ans = max (ans, mxx + mx);
}
int main(){
scanf("%d", &n);
for(int i=; i<=n; i++){
scanf("%d", &a[i]);
}
for(int i=; i<n; i++){
int u,v;
scanf("%d%d", &u, &v);
mp[u].pb(v);
mp[v].pb(u);
}
int cnt = ;
for(int i=; i<=n; i++){
if(!vis[i]){
dfs(i , cnt++);
}
}
for(int i=; i<=n; i++){
for(int k = ; k < mp[i].size(); k++){
if(col[i] != col[mp[i][k]]){
newmap[col[i]].pb(col[mp[i][k]]);
}
}
}
dfs2(, -);
printf("%d\n", (ans + )/);
return ;
}
CF734E
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 树的直径
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
题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...
- Codeforces 734E Anton and Tree(缩点+树的直径)
题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...
- 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 ...
随机推荐
- .net持续集成sonarqube篇之 sonarqube与jenkins集成(插件模式)
系列目录 Jenkins通过插件集成Sonarqube 通过上一节我们了解了如何配置以使jenkins ci环境中可以执行sonarqube构建,其实Sonarqube官方也提供了jenkins插件以 ...
- 【Maven】Mac 使用 zsh 后 mvn 命令就无效
RT -- 解决方法: 打开 .zshrc 文件,将 Maven 环境变量配置加入其中,或者 将 source ~/.bash_profile 添加到 .zshrc 中. PS: 之前搞不懂,每次使用 ...
- 【Android】java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has android.permission.READ_PHONE_STATE
RT, 异常信息如下: java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has and ...
- 8天入门docker系列 —— 第八天 让程序跑在swarm集群上
真正的落地部署都是希望程序跑在集群下,而不是单机版下测测玩玩,所以这篇就来聊一下怎么使用docker swarm进行部署,因为是swarm是docker自带的, 所以部署起来还是非常简单的. 一:前置 ...
- 云计算网络基础笔记及VLAN交换机配置
- Zookeeper 学习笔记(一)之功能介绍
Zookeeper 主要在以下场景中可以使用 一,命名服务(用到了zookeeper的文件系统) 命名服务是指通过指定的名字来获取资源或者服务的地址,利用zk创建一个全局的路径,提供服务的地址或者一个 ...
- Jquery事件和选择器 纠错
1: 试题分析:该题考的是jQuery中事件绑定的知识.绑定事件用bind()方法,选项A是正确的绑定事件语法,因此是正确的答案.选项BCD的语法是错误的. 2: 试题分析:opacity 必需.规定 ...
- .NET----错误和异常处理机制
前言 错误的出现并不总是编写程序的人的原因,有时应用程序会因为应用程序的最终用户引发的动作或运行代码的环境发生错误.无论如何,我们都应预测应用程序中出现的错误,并相应的进行编码. .Net改进了处理错 ...
- Web项目如何做单元测试
你可能会用单元测试框架,python的unittest.pytest,Java的Junit.testNG等. 那么你会做单元测试么!当然了,这有什么难的? test_demo.py def inc(x ...
- PIP键盘设置实时时钟--智能模块
大家好,许久没来发帖,今天带来点干货.希望大家多多讨论,相互学习. 使用 TOPWAY Smart LCD (HMT050CC-C) PIP键盘设置实时时钟 第一步 建立工程 第二步 建立2 ...