传送门:>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. Contest1692 - 2019寒假集训第三十一场 UPC 11075 Problem D 小P的国际象棋

    非常简单的单点修改+区间加+区间查询.我用的是最近刚学的区间修改版本树状数组.  直接维护即可,注意修改后的单点值已经不是a[i],或者b[i],要通过区间查询求单点.不然是错的. 区间修改版本树状数 ...

  2. 1003: [ZJOI2006]物流运输 = DP+SBFA

    题意就是告诉你有n个点,e条边,m天,每天都会从起点到终点走一次最短路,但是有些点在某些时间段是不可走的,因此在某些天需要改变路径,每次改变路径的成本是K,总成本=n天运输路线长度之和+K*改变运输路 ...

  3. poj3468 线段树的懒惰标记

    题目链接:poj3468 题意:给定一段数组,有两种操作,一种是给某段区间加c,另一种是查询一段区间的和 思路:暴力的方法是每次都给这段区间的点加c,查询也遍历一遍区间,复杂度是n*n,肯定过不去,另 ...

  4. prime算法

    参考博客:https://blog.csdn.net/lqcsp/article/details/14118871 复杂度:O(n*n) #include <iostream> #incl ...

  5. JSP页面的基本元素

    JSP页面元素构成:静态内容.指令.表达式.小脚本.声明.注释. JSP指令包括: page指令:通常位于jsp页面的顶端,同一个页面可以有多个page指令. include指令:将一个外部文件嵌入到 ...

  6. PS 十分钟教你做出文字穿插效果

  7. oc之考试答题类效果

    https://www.jianshu.com/p/ec29feb0b5a6 2017.07.27 11:48* 字数 424 阅读 615评论 9喜欢 11 demo地址:https://githu ...

  8. RabbitMQ防止消息丢失

    转载请注明出处 0.目录 RabbitMQ-从基础到实战(1)— Hello RabbitMQ RabbitMQ-从基础到实战(3)— 消息的交换 1.简介 RabbitMQ中,消息丢失可以简单的分为 ...

  9. 软件工程(FZU2015) 赛季得分榜,第七回合

    SE_FZU目录:1 2 3 4 5 6 7 8 9 10 11 12 13 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分=团队得分+个人贡献分 个人贡献分: 个人 ...

  10. semantic-ui 容器与栅格

    semantic中可以指定one-sixteen这16个单词来指定网格column所占的长度.也就是说,在网页中,一行最多只有16个column,超过16个之后,自动移到下一行. 栅格可以使用i,di ...