CodeForces 734E Anton and Tree
$dfs$缩点,树形$dp$。
首先将连通块缩点,缩点后形成一个黑白节点相间的树。接下来的任务就是寻找一个$root$,使这棵树以$root$为根,树的高度是最小的(也就是一层一层染色)。树形$dp$可以解决这个问题,第一次$dfs$处理子树,第二次$dfs$枚举$root$计算答案。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int n;
int c[];
int nc[];
int u[];
int v[];
int belong[];
int block;
vector<int>G[];
int flag[];
vector<int>L[],R[];
int ans; int a[]; void dfs(int x)
{
belong[x]=block;
for(int i=;i<G[x].size();i++)
{
int to=G[x][i];
if(c[to]!=c[x]) continue;
if(belong[to]!=) continue;
dfs(to);
}
} void dp(int x)
{
flag[x]=; int mx=;
for(int i=;i<G[x].size();i++)
{
int to=G[x][i];
if(flag[to])
{
L[x].push_back(mx);
continue;
} dp(to);
mx=max(mx,a[to]);
L[x].push_back(mx+);
}
a[x]=mx+;
} void dp2(int x)
{
flag[x]=; int mx=;
for(int i=G[x].size()-;i>=;i--)
{
int to=G[x][i];
if(flag[to])
{
R[x].push_back(mx);
continue;
} dp2(to);
mx=max(mx,a[to]);
R[x].push_back(mx+);
}
a[x]=mx+;
} void Find(int x,int h)
{
flag[x]=; int nh;
int sz=G[x].size();
for(int i=;i<G[x].size();i++)
{
int root=G[x][i];
if(flag[root]) continue; nh=h+; int k=;
if(i->=) k=max(k,L[x][i-]);
if(sz-i->=) k=max(k,R[x][sz-i-]);
if(k!=) nh=max(nh,k+); ans=min(ans,max(nh,a[root]));
Find(root,nh);
}
} int main()
{
cin>>n;
for(int i=;i<=n;i++) cin>>c[i];
for(int i=;i<=n-;i++)
{
cin>>u[i]>>v[i];
G[u[i]].push_back(v[i]);
G[v[i]].push_back(u[i]);
} for(int i=;i<=n;i++)
{
if(belong[i]!=) continue;
block++; nc[block]=c[i]; dfs(i);
} for(int i=;i<=n;i++) G[i].clear(); for(int i=;i<=n-;i++)
{
if(c[u[i]]==c[v[i]]) continue;
G[belong[u[i]]].push_back(belong[v[i]]);
G[belong[v[i]]].push_back(belong[u[i]]);
} memset(flag,,sizeof flag); dp();
memset(flag,,sizeof flag); dp2(); ans=a[];
memset(flag,,sizeof flag); Find(,); printf("%d\n",ans-); return ;
}
CodeForces 734E Anton and Tree的更多相关文章
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- 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 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- 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 ...
- 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 ...
- Anton and Tree
Anton and Tree 题目链接:http://codeforces.com/contest/734/problem/E DFS/BFS 每一次操作都可以使连通的结点变色,所以可以将连通的点缩成 ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
随机推荐
- poi-对于word的操作(二)
poi对于word文本的底纹和下划线的样式的展现 package poi.test; import java.io.FileOutputStream; import java.math.BigInte ...
- html 让一行文字居中
文本在行高范围内垂直居中 可以利用行高特性让一行文本居中 line-height:100px://父容器的高度
- 51nod 1806 wangyurzee的树
基准时间限制:1 秒 空间限制:131072 KB wangyurzee有n个各不相同的节点,编号从1到n.wangyurzee想在它们之间连n-1条边,从而使它们成为一棵树.可是wangyur ...
- 【51NOD-0】1058 N的阶乘的长度
[算法]数学 [题解]n!的位数相当于ans=log10(n!)上取整,然后就可以拆出来加了. 可以用log10(i)或log(i)/log(10) 阶乘好像有个斯特林公式…… #include< ...
- 从C语言项目谈编程
很多初学C语言的小伙伴,在学习之初并没有一个大概的概念,学习这门语言需要掌握多少知识点,怎么才算学的差不多? C语言的精髓点在哪? 学到多少东西才能够达到做项目的标准?学习的时候需要注意哪些细节点?疑 ...
- div 超出高度滚动条,超出宽度点点点
超出高度滚动条 style="width:230px; height: 180px; overflow: auto;" 超出宽度点点点 style="width: 220 ...
- sql 自定义split
以下数据库操作针对sql server. 问题来源:由于项目中,有的表字段内容是由多个id或多个其他内容拼接而成.(如:'1,2,3,4,5',或者'name_age_school'),特点是都用某个 ...
- [New learn]AutoLayout调查基于code
代码https://github.com/xufeng79x/TestAutolayout-code2 0.插在前面 必须关闭view的自动缩放掩码,自动缩放掩码是autolayout出现之前系统管理 ...
- python manage.py 命令
在用命令django‐admin.py startproject <工程目录>建立一个django工程文件时,会生成一个manage.py文件,那么这个manage.py到底可以干嘛呢? ...
- IE6下面的hover不兼容
第一种解决方法: ie6中hover只是对a标签有作用 必须有href=“”,否则都不管用,如果不能写a标签,还想让ie6下有滑过效果,那只能写javascript或者jquery. 例如:ie6是不 ...