传送门

题意:

这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色。问最少操作次数,使得树上颜色相同。

思路:

先缩点,把相同的颜色的相邻节点缩在一起。再求出树的最长直径S(边的个数),答案就是(S + 1)/ 2;

因为对于一条链,我们可以从中间向两边交换改变。

  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. using namespace std;
  4.  
  5. const int MAXN = 2e5+;
  6. vector<int>mp[MAXN],newmap[MAXN];
  7. int n,a[MAXN],col[MAXN],h[MAXN],fa[MAXN];
  8. bool vis[MAXN];
  9. int ans = ;
  10. void dfs(int u, int c){
  11. vis[u] = true;
  12. col[u] = c;
  13. for(int i=; i < mp[u].size(); i++){
  14. int v = mp[u][i];
  15. if(a[v]!=a[u])continue;
  16. if(vis[v])continue;
  17. dfs(v,c);
  18. }
  19. }
  20. void dfs2(int u, int o){
  21.  
  22. int mx = ,mxx = ;
  23. for(int i=; i<newmap[u].size(); i++){
  24. int v = newmap[u][i];
  25. if(v==o)continue;
  26. dfs2(v,u);
  27. mx = max(mx,h[v] + );
  28. if(mx > mxx)swap(mx,mxx);
  29. }
  30. h[u] = mxx;
  31. ans = max (ans, mxx + mx);
  32. }
  33. int main(){
  34. scanf("%d", &n);
  35. for(int i=; i<=n; i++){
  36. scanf("%d", &a[i]);
  37. }
  38. for(int i=; i<n; i++){
  39. int u,v;
  40. scanf("%d%d", &u, &v);
  41. mp[u].pb(v);
  42. mp[v].pb(u);
  43. }
  44. int cnt = ;
  45. for(int i=; i<=n; i++){
  46. if(!vis[i]){
  47. dfs(i , cnt++);
  48. }
  49. }
  50. for(int i=; i<=n; i++){
  51. for(int k = ; k < mp[i].size(); k++){
  52. if(col[i] != col[mp[i][k]]){
  53. newmap[col[i]].pb(col[mp[i][k]]);
  54. }
  55. }
  56. }
  57. dfs2(, -);
  58. printf("%d\n", (ans + )/);
  59. return ;
  60. }

CF734E

Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #379 (Div. 2) E. Anton and Tree

    题意: 给一颗树 每个节点有黑白2色 可以使一个色块同事变色,问最少的变色次数. 思路: 先缩点 把一样颜色的相邻点 缩成一个 然后新的树 刚好每一层是一个颜色. 最后的答案就是树的直径/2 不过我用 ...

  5. Codeforces 734E Anton and Tree(缩点+树的直径)

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  9. 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 ...

随机推荐

  1. 基于简单DUT的UVM验证平台的搭建(一)

    最近一个月在实习公司做回归测试,对公司的UVM平台用的比较熟练,就想着自己做一个DUT,然后搭建一个UVM验证平台. 首先,DUT是一个简单的32位的加法器,代码如下:alu.v module add ...

  2. 【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 ...

  3. Qt基于sqlite数据库的管理小软件

    闲来无事,写了一个基于sqlite的数据库管理小软件. 先上图 中心思想就是: 创建一个数据库 然后每一个分组对应一个数据表 然后遍历该数据表.将名字以treewidgetItem显示出来.添加删除实 ...

  4. 9.源码分析---SOFARPC是如何实现故障剔除的?

    SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...

  5. Zookeeeper应用实践(四)

    zk的应用还是非常广泛的. 1. 分布式锁 单机环境下的锁还是很容易去实现的,但是在分布式环境下一切都变得不是那么简单.zk实现分布式锁的原理还简单,因为在分布式环境中的zk节点的变化会被每一台机器w ...

  6. WebSphere MQ性能调优浅谈

    导读:目前随着我们在中国的WebSphere MQ(MQSeries)用户数量越来越多,越来越多的用户开始对MQ使用时的性能优化问题提出要求,我根据日常积累的经验谈一谈在MQ性能优化方面应该考虑的因素 ...

  7. C语言tips_1 关于&& || ! 的优先级

    关于&& || ! 三种操作的优先级 测试如下 简要分析 假设&&>|| 则结果为1 假设||>&& 则结果为0 结果为1 得证 & ...

  8. (二十一)c#Winform自定义控件-气泡提示

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. 从零写一个编译器(十一):代码生成之Java字节码基础

    项目的完整代码在 C2j-Compiler 前言 第十一篇,终于要进入代码生成部分了,但是但是在此之前,因为我们要做的是C语言到字节码的编译,所以自然要了解一些字节码,但是由于C语言比较简单,所以只需 ...

  10. Django-内置用户系统

    Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括 1.用户注册 2.用户登录 3.用户认证 4.注销 5.修改密码 Django作为一个 ...