题目大意:

给定一棵有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. [Sdoi2013]随机数生成器(BSGS)

    #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...

  2. nginx中使用waf防火墙

    1.安装依赖 yum install -y readline-devel ncurses-devel 2.安装Lua # .tar.gz # cd lua- # make linux # make i ...

  3. iOS-事件传递和响应机制

    转自:http://www.jianshu.com/p/2e074db792ba 前言: 按照时间顺序,事件的生命周期是这样的: 事件的产生和传递(事件如何从父控件传递到子控件并寻找到最合适的view ...

  4. 【python】-- Redis简介、命令、示例

    Redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化 ...

  5. Webpack探索【4】--- entry和output详解

    本文主要讲entry和output相关内容.

  6. vue 计算属性和监听器

    一.计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split('').rev ...

  7. cocos2d-x3.6 生成带类图的离线文档

    我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...

  8. windows10下载

    http://care.dlservice.microsoft.com/dl/download/F/5/7/F574727C-B145-4A7D-B85B-11C4E8DC894B/9841.0.14 ...

  9. ppm图像相关

    PPM图像格式介绍 直接拿具体的数据来说明是最直接的,使用ue打开ppm文件,采用的都是十六进制asc码表示的,这里要注意地址00000000h中的最后一个字母是始终不变的,这原来没注意晕了我好久,第 ...

  10. python 统计单词出现次数

    #use python3.6 import re from collections import Counter FILESOURCE = './abc.txt' def getMostCommonW ...