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 ...
随机推荐
- Python3的日志添加功能
python日志添加功能,主要记录程序运行中的日志,统一收集并分析 一.日志的级别 debug(调试信息) info() warning(警告信息)error(错误信息) critical(致命信息) ...
- QTableView表格控件区域选择-自绘选择区域
目录 一.开心一刻 二.概述 三.效果展示 四.实现思路 1.绘制区域 2.绘制边框 3.绘制 五.相关文章 原文链接:QTableView表格控件区域选择-自绘选择区域 一.开心一刻 陪完客户回到家 ...
- C#中属性的解析
一.域的概念 C#中域是指成员变量和方法,在OOP编程中(面向对象编程)我们要求用户只知道类是干什么的,而不许知道如何完成的,或者说不允许访问类的内部,对于有必要在类外可见的域,我们用属性来表达,所以 ...
- apache bench的简单使用
ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchmark testing,可以同时模拟多个并发请求. 需要针对web做压力测试,所以简单学习了一 ...
- 【React踩坑记二】react项目实现JS路由跳转
这里使用的是4.31版本的react-router-dom "react-router-dom": "^4.3.1", 直接使用以下代码即可实现路由跳转 thi ...
- linux CPU100%异常排查
1.top查找出占CPU比例最高的进程(5881): 2.查看该进程正在执行的线程: top -H -p 5881 3.将线程转换成16进制 printf ‘%x\n’ 5950 4.查看异常线程执 ...
- Spring基础笔记
Spring带给了我们什么便利? 注解版本的IOC如何玩? 组件注册 组件注册的过程中有哪些过滤规则? 如何控制组件的作用域(单例多例)? 六种注册组件的方式? 生命周期 什么是bean的生命周期 在 ...
- Tomcat中文乱码问题
新从官网下载的Tomcat7和Tomcat8,在运行的时候都会有乱码的问题,就此发现问题,我们就给它就地正法! 经过初步的分析,问题产生的大概原因是由于Tomcat的log日志模块不识别中文的问题, ...
- spring boot 打 war包
spring boot .spring cloud打 war包,并发布到tomcat中运行 1.pom文件修改 <packaging>war</packaging> 2.< ...
- 云上RDS架构
概述 越来越多的企业选择上云,最基础的云服务就是IaaS(Infrastructure as a Service)服务,直观理解就是虚拟主机,用户不用再自建机房,自己购买服务器,而是直接向云厂商购买虚 ...