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 ...
随机推荐
- 基于简单DUT的UVM验证平台的搭建(一)
最近一个月在实习公司做回归测试,对公司的UVM平台用的比较熟练,就想着自己做一个DUT,然后搭建一个UVM验证平台. 首先,DUT是一个简单的32位的加法器,代码如下:alu.v module add ...
- 【Android Studio】E/memtrack: Couldn't load memtrack module (No such file or directory)【待解决】
Android Studio 又遇到了问题--如下: 06-21 07:27:57.855 3232-3232/? E/memtrack: Couldn't load memtrack module ...
- Qt基于sqlite数据库的管理小软件
闲来无事,写了一个基于sqlite的数据库管理小软件. 先上图 中心思想就是: 创建一个数据库 然后每一个分组对应一个数据表 然后遍历该数据表.将名字以treewidgetItem显示出来.添加删除实 ...
- 9.源码分析---SOFARPC是如何实现故障剔除的?
SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...
- Zookeeeper应用实践(四)
zk的应用还是非常广泛的. 1. 分布式锁 单机环境下的锁还是很容易去实现的,但是在分布式环境下一切都变得不是那么简单.zk实现分布式锁的原理还简单,因为在分布式环境中的zk节点的变化会被每一台机器w ...
- WebSphere MQ性能调优浅谈
导读:目前随着我们在中国的WebSphere MQ(MQSeries)用户数量越来越多,越来越多的用户开始对MQ使用时的性能优化问题提出要求,我根据日常积累的经验谈一谈在MQ性能优化方面应该考虑的因素 ...
- C语言tips_1 关于&& || ! 的优先级
关于&& || ! 三种操作的优先级 测试如下 简要分析 假设&&>|| 则结果为1 假设||>&& 则结果为0 结果为1 得证 & ...
- (二十一)c#Winform自定义控件-气泡提示
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- 从零写一个编译器(十一):代码生成之Java字节码基础
项目的完整代码在 C2j-Compiler 前言 第十一篇,终于要进入代码生成部分了,但是但是在此之前,因为我们要做的是C语言到字节码的编译,所以自然要了解一些字节码,但是由于C语言比较简单,所以只需 ...
- Django-内置用户系统
Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括 1.用户注册 2.用户登录 3.用户认证 4.注销 5.修改密码 Django作为一个 ...