CodeForces-734E Anton and Tree 树的直径
题目大意:
给定一棵有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 树的直径的更多相关文章
- Codeforces 734E Anton and Tree(缩点+树的直径)
题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...
- 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 ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- Codeforces 379F New Year Tree 树的直径的性质推理
New Year Tree 我们假设当前的直径两端为A, B, 那么现在加入v的两个儿子x, y. 求直径的话我们可以第一次dfs找到最远点这个点必定为直径上的点, 然而用这个点第二次dfs找到最远点 ...
- CodeForces 734E Anton and Tree
$dfs$缩点,树形$dp$. 首先将连通块缩点,缩点后形成一个黑白节点相间的树.接下来的任务就是寻找一个$root$,使这棵树以$root$为根,树的高度是最小的(也就是一层一层染色).树形$dp$ ...
- 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 ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- 【bzoj2870】最长道路tree 树的直径+并查集
题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...
- codeforces 14D(搜索+求树的直径模板)
D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...
随机推荐
- web 文件下载
response.reset(); response.setContentType("octets/stream"); response.addHeader("Conte ...
- URAL 1181 Cutting a Painted Polygon【递归+分治】
题目: http://acm.timus.ru/problem.aspx?space=1&num=1181 http://acm.hust.edu.cn/vjudge/contest/view ...
- 广告 竞价排名 import Levenshtein as Le seqratio_res = Le.seqratio(chk_name_lsit, cmp_)
pip install python-Levenshtein from openpyxl import Workbook import xlrd import time import Levensht ...
- javascript中apply和call的区别
请补充 136页 pdf 高级javascript设计
- 路由helper
root_url http://192.168.1.110:3000/users/sign_up?inviter=14658733081530 root_path /users/sign_up?inv ...
- rewrite_static
<?php class MyObject { public static $myStaticVar = 0; function myMethod() { self::$myStaticVar + ...
- 3.07课·········if分支语句
语句分类:顺序语句,选择语句(分支语句),循环语句 分支语句:(一)if(表达式) //表达式返回值是True或False{}说明:1.表达式返回的是bool值:2.小括号和花括号后面不需要加分号. ...
- Eclipse cdt mingw配置记录
本人下载的是Eclipse C/C++ IDE for Neon.3,下载页面是:http://www.eclipse.org/cdt/downloads.php. 1. 运行eclipse后,在He ...
- 浮动float
一.浮动定义:会强制改变display为inline-block,使元素排队列,(排列方式由属性值决定,left / rihgt), 使该元素处于半脱离文档状态 二.浮动元素处于半飘离状态,能看到浮动 ...
- 面试问题(HTML和CSS方面)
1 IE/Win的 HasLayout 2 浮动 float 的定义.float后元素的display属性会发生改变吗?3 CSS 3.0.CSS2.1 中被现代浏览器应用了的规则有哪些?4 父元素定 ...