传送门:>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. Go+Python双剑合璧

    目的 Python调用Go的方法,Python有很多功能强悍又使用简洁的库.而新生军Go的多核心利用率也是非常强悍的.当然这是明面上的优点.反正你有很多理由想要让Python能够调用Go的方法. 实验 ...

  2. Python学习第三篇——访问列表部分元素

    dongman =["huoying","sishen","si wang bi ji","pan ni de lu lu xiu ...

  3. UnderWater+SDN论文之四

    Open Source Suites for Underwater Networking:WOSS and DESERT Underwater Source: IEEE Network, 2014 仿 ...

  4. 使用faker去构造一个User-Agent

    faker可以仿造各种各样的信息,可以使用faker去构造一个User-Agent from faker import Factory f = Factory.create() 'User-Agent ...

  5. Day12 Python基础之生成器、迭代器(高级函数)(十)

    https://www.cnblogs.com/yuanchenqi/articles/5769491.html 1. 列表生成式 我现在有个需求,看列表[0, 1, 2, 3, 4, 5, 6, 7 ...

  6. (poj 2253) Frogger 最短路上的最大路段

    题目链接:http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of a ...

  7. setState的参数接收函数

  8. [转帖]Linux分页机制之概述--Linux内存管理(六)

    Linux分页机制之概述--Linux内存管理(六) 2016年09月01日 19:46:08 JeanCheng 阅读数:5491 标签: linuxkernel内存管理分页架构更多 个人分类: ┈ ...

  9. CSS3 background-size属性兼容

    background-size是CSS3新增的属性,但是IE8以下还是不支持 background-size:contain; // 缩小图片来适应元素的尺寸(保持像素的长宽比):background ...

  10. C# Note18: 使用wpf制作about dialog(关于对话框)

    前言 基本上任何software或application都会在help菜单中,有着一个关于对话框,介绍产品的版权.版本等信息,还有就是对第三方的引用(add author credits). 首先,看 ...