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 ...
随机推荐
- 【iOS】NSLog 打印 BOOL 类型值
这个问题以前没在意,刚偶然打印,发现有些问题,上网查了下,发现是这么搞的: NSLog(@"%@", isEqual?@"YES":@"NO" ...
- myeclipse中从svn下载的web工程,到工作空间却显示成Java工程
转载自:https://blog.csdn.net/u011217058/article/details/57970587 右键工程,properties-> Project Facets-&g ...
- 几大排序算法的Java实现(原创)
几大排序算法的Java实现 更新中... 注: 该类中附有随机生成[min, max)范围不重复整数的方法,如果各位看官对此方法有什么更好的建议,欢迎提出交流. 各个算法的思路都写在该类的注释中了,同 ...
- restapi(4)- rest-mongo : MongoDB数据库前端的httpserver
完成了一套标准的rest风格数据库CRUD操作httpserver后发现有许多不足.主要是为了追求“通用”两个字,想把所有服务接口做的更“范generic”些,结果反而限制了目标数据库的特点,最终产生 ...
- Go orm框架gorm学习
之前咱们学习过原生的Go连接MYSQL的方法,使用Go自带的"database/sql"数据库连接api,"github.com/go-sql-driver/mysql& ...
- Linux系统与程序监控工具atop教程
引言 Linux以其稳定性,越来越多地被用作服务器的操作系统(当然,有人会较真地说一句:Linux只是操作系统内核:).但使用了Linux作为底层的操作系统,是否我们就能保证我们的服务做到7*24地稳 ...
- Mermaid
graph TD; A-->B; A-->C; B-->D; C-->D;
- 深入理解struts的运行机制
扫码关注公众号,不定期更新干活 在此申明本博文并非原创,原文:http://blog.csdn.net/lenotang/article/details/3336623,本文章是在此文章基础上进行优化 ...
- Linux之vim详解
第一次使用vim,啥都不懂,输入也不能输入,退出也不会退出,特别的尴尬....后来慢慢的接触学习,发现vim真的挺好用的,不过上手有点慢,多用就对了,用多了我相信你也会喜欢这个文本编辑工具的 一.vi ...
- thinkphp 框架两种模式 两种模式:开发调试模式、线上生产模式
define(‘APP_DEBUG’,true/false);