题目大意:

给定一棵有n个节点的树,有黑点白点两种节点.

每一次操作可以选择一个同种颜色的联通块将其染成同一种颜色

现在给定一个初始局面问最少多少步可以让树变为纯色.

题解:

首先我们拿到这棵树时先将其缩点

然后我们手中的树就变成了一棵黑白相间的黑白树.

那么我们现在就是每次选择一个节点使其变色,都会使得这个节点相邻的所有节点合并进来.

所以我们找度数最大的合并就好了啊

我们现在把这棵树想象成由若干条路径组成的.

那么我们每次合并都会使某些路径的长度最多减少2

所以我们可以自然而然地想到一定是树的直径花费的操作次数最大.

所以我们将一棵树化作一条链上面连着许多其他的分支的形式.

手模几个样例就话发现答案实际上是\([\frac{len+1}{2}]\)len为直径长度.

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 200010;
int belong[maxn],nodecnt;
int col[maxn];
struct Graph{
struct Edge{
int to,next;
}G[maxn<<1];
int head[maxn],cnt;
void add(int u,int v){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
}
#define v G[i].to
void dfs1(int u,int f){
if(col[u] != col[f]) belong[u] = ++ nodecnt;
else belong[u] = belong[f];
for(int i = head[u];i;i=G[i].next){
if(v == f) continue;
dfs1(v,u);
}return ;
}
int dis[maxn],p;
void dfs2(int u,int f){
for(int i = head[u];i;i=G[i].next){
if(v == f) continue;
dis[v] = dis[u] + 1;
dfs2(v,u);
}
if(dis[p] < dis[u]) p = u;
}
#undef v
}g1,g2;
int main(){
int n;read(n);
for(int i=1;i<=n;++i) read(col[i]);
for(int i=1,u,v;i<n;++i){
read(u);read(v);
g1.add(u,v);g1.add(v,u);
}belong[1] = ++ nodecnt;
g1.dfs1(1,1);
for(int u=1;u<=n;++u){
for(int i = g1.head[u];i;i=g1.G[i].next){
if(belong[g1.G[i].to] != belong[u]){
g2.add(belong[u],belong[g1.G[i].to]);
}
}
}
g2.dfs2(1,1);
int u = g2.p;
memset(g2.dis,0,sizeof g2.dis);
g2.dfs2(u,u);
int ans = (g2.dis[g2.p] + 1)/2;
printf("%d\n",ans);
return 0;
}

CodeForces-734E Anton and Tree 树的直径的更多相关文章

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

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

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

  3. Codeforces 734E. Anton and Tree 搜索

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

  4. Codeforces 379F New Year Tree 树的直径的性质推理

    New Year Tree 我们假设当前的直径两端为A, B, 那么现在加入v的两个儿子x, y. 求直径的话我们可以第一次dfs找到最远点这个点必定为直径上的点, 然而用这个点第二次dfs找到最远点 ...

  5. CodeForces 734E Anton and Tree

    $dfs$缩点,树形$dp$. 首先将连通块缩点,缩点后形成一个黑白节点相间的树.接下来的任务就是寻找一个$root$,使这棵树以$root$为根,树的高度是最小的(也就是一层一层染色).树形$dp$ ...

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

  7. LightOJ1094 - Farthest Nodes in a Tree(树的直径)

    http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...

  8. 【bzoj2870】最长道路tree 树的直径+并查集

    题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...

  9. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

随机推荐

  1. java对IO的操作

    import java.io.*; public class HelloWorld { //Main method. public static void main(String[] args) { ...

  2. python基础11 ---函数模块1

    函数模块 一.函数模块的作用(为什么要有函数模块) 1.函数模块可以减少代码量 2.函数模块方便阅读 3.函数模块维护性强二.函数模块的本质以及调用方法 1.函数模块的本质就是一个.py结尾的文件,该 ...

  3. iBatis 简单介绍及基础入门

    iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2002年发起的开放源代码项目.于2010年6月16号被谷歌托管,改名为MyBatis.是一个基 ...

  4. 牛客小白月赛1 C 分元宵【快速幂】

    题目链接 https://www.nowcoder.com/acm/contest/85/C 思路 有 A 种 元宵馅,B 种元宵皮 所以 我们可以认为 有Q = A * B 种 元宵 有 C 张桌子 ...

  5. 深入理解ES6之迭代器与生成器

    迭代器 迭代器 iterator,在 Javascript 中,迭代器是一个对象(也可称作为迭代器对象),它提供了一个 next() 方法,用来返回迭代序列中的下一项. next 方法的定义,next ...

  6. table-cell笔记

    display:table-cell可将元素设为类似于table的td一样的布局,在垂直居中.两行自适应布局.等高布局下有很高的利用价值 详见: http://www.zhangxinxu.com/w ...

  7. poj 3126 Prime Path 【bfs】

    题目地址:http://poj.org/problem?id=3126 Input One line with a positive number: the number of test cases ...

  8. 百度编辑器 Ueditor 如何增加字体 ?

    在百度编辑器 Ueditor 如何增加字体 ? 要修改两个文件: 第一个文件:editor-config.js: ,'fontfamily':[             { label:'',name ...

  9. 在Delphi中使用ShellExecute(handle, 'open', PChar(fname),nil, nil, SW_HIDE)函数应注意的问题

    在Delphi中使用ShellExecute(handle, 'open', PChar(fname),nil, nil, SW_HIDE)函数应注意的问题: 一.对一般vcl程序及isapi dll ...

  10. 本地连接出现"已启用检测该状态的服务"解决方法、方案

    1.运行 输出dcomcnfg 2.组件服务-计算机-我的电脑-DCOM配置-netprofm 3.右键属性-安全-启动和激活权限-自定义 4.编辑-添加-输入对象名称来选择-输入“LOCAL SER ...