题目大意:

给定一棵有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. 关于function的一种常用用法

    关于function的一种常用用法 void Share::InitAcrossManager() { GsMgrEvent gsMgrEvents;//保存function的结构体 gsMgrEve ...

  2. javascript修改图片链接地址

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  3. J - 组合

    J - 组合 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Description 有两 ...

  4. android菜鸟学习笔记18----Android数据存储(二)SharedPreferences

    数据存储的方式,有比直接文件读写更加简便的方式,那就是操作SharedPreferences. SharedPreferences一般用于存储用户的偏好设定,暂时不支持多进程操作. SharedPre ...

  5. 7.FactoryBean 和BeanFactory去区别

    FactoryBean源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apach ...

  6. What Every Computer Scientist Should Know About Floating-Point Arithmetic

    http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

  7. word2vec_basic.py

    ssh://sci@192.168.67.128:22/usr/bin/python3 -u /home/win_pymine_clean/feature_wifi/word2vec_basic.py ...

  8. 学院名单-211院校研招学院-中国教育在线(www.eol.cn)170915164402

    [数据结果] 学校数.学院数:112,2657. [数据来源] 中国教育在线(www.eol.cn)211院校研招学院. http://www.eol.cn/html/ky/gxmd/211.shtm ...

  9. rm_invalid_file

    import xlrd import time import sys import os import requests import sqlite3 import threading curPath ...

  10. 基于 django 自带的用户认证进行用户认证

    django admin 默认已经存在了一个用户认证,这个时候可以偷个小懒,直接用 django 自带的,就不需要自己写用户认证了 1.目录结构: 2.代码 在 settings.py 中添加一行 # ...