传送门:>Here<

题意:给出一颗树,节点不是黑色就是白色,每次可以将一个颜色相同的块变颜色,问最少变几次才能让其变为同色

解题思路:

  我们考虑由于每一次都是把同样颜色的色块进行变色,因此同样颜色的色块可以看成一个点。所以我们先将同一个色块缩成一个点。

  然后我们有一个结论,我们最后的答案就是缩点完成的这棵树的直径+1再除以2.

  我们很容易发现,缩点完成以后的树相邻的两个点颜色一定是不同的,否则就能继续缩。因此我们可以每次选择直径中间的那个点,改变它的颜色,然后它就与周围的那些点融合成为一个新的点,然后再找到中间的,继续重复如上步骤。最后我们会发现,恰好是$(dis+1) / 2$次。这个证明不是很严谨,不过感性地理解一下吧

Code

  细节不多

/*by DennyQi*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define r read()
#define Max(a,b) (((a)>(b))?(a):(b))
#define Min(a,b) (((a)<(b))?(a):(b))
using namespace std;
typedef long long ll;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
inline int read(){
int x = ; int w = ; register unsigned char c = getchar();
for(; c^'-' && (c < '' || c > ''); c = getchar());
if(c == '-') w = -, c = getchar();
for(; c >= '' && c <= ''; c = getchar()) x = (x<<) + (x<<) + c - '';
return x * w;
}
int N,cur_num;
int color[MAXN],nod[MAXN],x[MAXN],y[MAXN],d[MAXN],vis[MAXN];
vector <int> g[MAXN],G[MAXN];
queue <int> q;
inline void add(int u, int v){
g[u].push_back(v);
}
inline void Add(int u, int v){
G[u].push_back(v);
}
void dfs(int u){
int sz = g[u].size(),v;
nod[u] = cur_num;
for(int i = ; i < sz; ++i){
v = g[u][i];
if(!nod[v] && (color[v] == color[u])){
dfs(v);
}
}
}
inline void BFS(int s){
memset(d, 0x3f, sizeof(d));
memset(vis,,sizeof(vis));
d[s] = ;
q.push(s);
vis[s] = ;
int u,sz,v;
while(!q.empty()){
u = q.front();q.pop();
sz = G[u].size();
for(int i = ; i < sz; ++i){
v = G[u][i];
if(!vis[v]){
vis[v] = ;
d[v] = d[u] + ;
q.push(v);
}
}
}
}
int main(){
// freopen(".in","r",stdin);
N=r;
for(int i = ; i <= N; ++i){
color[i]=r;
}
for(int i = ; i < N; ++i){
x[i]=r,y[i]=r;
add(x[i], y[i]);
add(y[i], x[i]);
}
for(int i = ; i <= N; ++i){
if(!nod[i]){
++cur_num;
dfs(i);
}
}
for(int i = ; i < N; ++i){
if(nod[x[i]] != nod[y[i]]){
Add(nod[x[i]], nod[y[i]]);
Add(nod[y[i]], nod[x[i]]);
}
}
/* for(int i = 1; i <= cur_num; ++i){
printf("%d: ", i);
for(int j = 0; j < G[i].size(); ++j){
printf("%d,",G[i][j]);
}
printf("\n");
}*/
BFS();
int _max = -,p;
for(int i = ; i <= cur_num; ++i){
if(d[i] > _max){
_max = d[i];
p = i;
}
}
/* printf("p = %d\n", p);
for(int i = 1; i <= cur_num; ++i){
printf("%d ",d[i]);
}
printf("\n");*/
BFS(p);
_max = -;
for(int i = ; i <= cur_num; ++i){
if(d[i] > _max){
_max = d[i];
}
}
/* for(int i = 1; i <= cur_num; ++i){
printf("%d ",d[i]);
}
printf("\n");
printf("_max = %d\n", _max);*/
printf("%d",(_max+)/);
return ;
}

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

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

  9. cf734 E. Anton and Tree

    这个题的题意还是很劲的.搞了好久才知道是怎么变得. (假设已经缩好了点,每次边中间颜色不同的,然后和就和他外面的相同,继续再变这个大的,依次类推,最多就是树的直径D/2) (还是英语水平太弱了(吐槽+ ...

随机推荐

  1. 编剧小记 — Contour

    前言 Contour 是一款比较优秀的编剧辅助软件,按理说这篇文章应该归类到mac小记中,但其操作非常简单,基本上以写作提示为主.只怪所有提示都是英语,而且很多,每次使用打开 Contour 个别单词 ...

  2. 数组去重--ES5和ES6

    思路:把去重后的数组放到一个空数组中 ES5实现: function uni(arr) { var result = []; for (var i=0;i<arr.length;i++) { i ...

  3. c++入门之类继承初步

    继承是面向对象的一种很重要的特性,先来复习基类的基本知识: 先上一段代码: # ifndef TABLE00_H # define TABLE00_H # include "string&q ...

  4. session与cookie的区别以及HTML5中WebStorage理解

    一.session与cookie的区别 二.HTML5中WebStorage理解 WebStorage的目的是克服由cookie所带来的一些限制,当数据需要被严格控制在客户端时,不需要持续的将数据发回 ...

  5. oc之脚本

    进入Build Phases页面,点击加号选择“New Run Script Phases”创建Run Script 在这里添加Run Script, 1.每次编译运行版本号(bundleVersio ...

  6. Eclipse启动错误JVM terminated. exit code 1解决方法

    现象: 前一天eclipse还用得好好的,但今天就不能用了,怎么回事? 解决方案: 请先参考其它网络资料:http://www.baidu.com/s?wd=eclipse+jvm+terminate ...

  7. vue动态class——实现tag的选中状态

    vue动态class——实现tag的选中状态 <template> <div class="common-nav"> <div class=" ...

  8. jenkins+maven+tomcat集群发布

    jenkins+Gitlab+maven+tomcat实现自动集成.打包.部署 - 李栋94 - 博客园https://www.cnblogs.com/lidong94/p/7427923.html ...

  9. 把composer的源切换为 国际的源

      把composer的源切换为 国际的源:composer config -g repo.packagist composer https://packagist.org

  10. [转帖]nginx配置ssl加密(单/双向认证、部分https)

    nginx配置ssl加密(单/双向认证.部分https) https://segmentfault.com/a/1190000002866627   nginx下配置ssl本来是很简单的,无论是去认证 ...