\(\mathtt{CF734E}\)

\(\mathcal{Description}\)

给一棵\(n(n\leq200000)\)个节点的树,每个点为黑色或白色,一次操作可以使一个相同颜色的连通块变成另一种颜色,求使整棵树变成一种颜色的最少操作数。

\(\mathcal{Solution}\)

这棵树中每个点为黑点或白点,然后最后也只要求出最小操作数,对于一个联通块,我们选择其中任何一个节点进行染色的效果是一样的(都会把这个联通块变成同一个颜色),于是我们自然而然的可以想到缩点。然后样例中的树就可以是

(贺个图\(qwq\))

可以看出我们把这个树从原先的树变成了一棵异层颜色相异(也就是黑白相间)的树,如果当前是一条链,我们要是最后操作数是最小的,我们一定会选择从中间开始染色,所以对于一棵树,我们只需要选择他的直径进行染色,最后的答案就是\((直径+1)/ 2\)。

\(\mathcal{Code}\)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10; int n, color[N];
vector<int> a[N]; inline int read() {
int x = 0, k = 1; char c = getchar();
for (; c < 48 || c > 57; c = getchar()) k ^= (c == '-');
for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48);
return k ? x : -x;
} pair<int, int> dfs(int x, int fa, int depth) {
int sz = a[x].size();
pair<int, int> tmp = make_pair(depth, x);
for (int i = 0; i < sz; i++) {
int y = a[x][i];
if (y == fa)
continue;
if(color[y] != color[x])
tmp = std::max(tmp, dfs(y, x, depth + 1));
else
tmp = std::max(tmp, dfs(y, x, depth));
}
return tmp;
} int main() {
n = read();
for (int i = 1; i <= n; i++)
color[i] = read();
for (int i = 1; i < n; i++) {
int x = read(), y = read();
a[x].push_back(y), a[y].push_back(x);
}
pair<int, int> tmp = dfs(1, -1, 0);
tmp = dfs(tmp.second, -1, 0);
printf("%d\n", tmp.first + 1 >> 1);
}

CF734E Anton and Tree的更多相关文章

  1. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

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

  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. Anton and Tree

    Anton and Tree 题目链接:http://codeforces.com/contest/734/problem/E DFS/BFS 每一次操作都可以使连通的结点变色,所以可以将连通的点缩成 ...

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

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

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

  7. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 树的直径

    传送门 题意: 这道题说的是在一颗有两种颜色的树上,每操作一个节点,可以改变这个节点颜色和相邻同色节点的颜色.问最少操作次数,使得树上颜色相同. 思路: 先缩点,把相同的颜色的相邻节点缩在一起.再求出 ...

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

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

随机推荐

  1. CH340电路设计

    版权声明:技术需要共享,但同时需要尊重原创者的辛劳,转载引用请注明出处. https://blog.csdn.net/JAZZSOLDIER/article/details/66967735 最近选用 ...

  2. java.sql.SQLException: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation '='

    查询视图时报错:java.sql.SQLException: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_ ...

  3. html{-webkit-text-size-adjust:none;}(取消浏览器最小字体限制)

    2016年10月13日 09:31:58 ITzhongzi 阅读数 9409   1.当样式表里font-size<12px时,中文版chrome浏览器里字体显示仍为12px,这时可以用 ht ...

  4. 常用跨域方法总结(2)——CORS

    常用跨域方法总结(2)--CORS 上篇文章介绍了几种常用的跨域方法:常用跨域方法总结,本片为上一篇的补充,对比较重要的Cross Origin Resource Sharing详细介绍. CORS ...

  5. tcp和udp得区别

    TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议 UDP 是User Datagram Protocol,即 用户 ...

  6. 開啟windows 7 ,10 的熱點功能(無線熱點)

    開啟windows 7 ,10 的熱點功能: 1.首先要確定你的電腦無線芯片有無熱點功能  # netsh wlan show drivers    Hosted network supported ...

  7. php开发面试题---攻击网站的常用手段有哪些,及如何预防(整理)

    php开发面试题---攻击网站的常用手段有哪些,及如何预防(整理) 一.总结 一句话总结: 比较记忆:注意比较各种攻击的区别,比如csrf和xss,以及xss和sql,这样才能记住 1.Sql注入是什 ...

  8. 没有找到MSVCR110.dll,因此这个应用程序未能启动.重新安装应用程序可能会修复此问题

    问题: 在win7下用vs2012编译了一个exe放到xp上运行,弹出错误框"没有找到MSVCR110.dll,因此这个应用程序未能启动.重新安装应用程序可能会修复此问题" 解决办 ...

  9. mysql存储过程、函数、触发器、

    当数据库版本不允许直接使用存储过程.函数的语法时用delimiter // 将结束符改成//用完之后再写delimiter;将结束符改回来即可,调用过程.函数用call+其名字即可返回结果 delim ...

  10. 1. 什么是Prometheus

    什么是Prometheus Prometheus是一个最初在SoundCloud上构建的开源系统监视和警报工具包 .自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有一个非常 ...