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 ...
随机推荐
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- EasyUI Tree递归方式获取JSON
最近需要用到EASYUI中的TREE功能,以前我是直接拼接成<UL><LI>发现这样拼完之后在更改树后对树的刷新不是很理想,现改用JSON格式,首先分析TREE中JOSN格式如 ...
- 2015/8/10 Python基本使用(1)
此文为<Python核心编程>的读书笔记记录. Python是一门解释性语言,所有的语句用解释器(interpreter)来直接解释,但它同时是High Level的语言,这样的组成能够在 ...
- 最简单的图文教程,几步完成Git的公私钥配置
操作的前提是,你有git账号,并且在自己的电脑上已经装好了TorToiseGit工具.下面开始简单的教程: 第一步:Windows-->所有程序-->TortoiseGit-->Pu ...
- 【Foreign】光 [莫比乌斯反演]
光 Time Limit: 10 Sec Memory Limit: 128 MB Description 天猫有一个长方形盒子,长宽分别为A,B. 这个长方形盒子的内壁全部是镜面. 天猫在这个盒子 ...
- 【codevs】3196 黄金宝藏
[算法]区间DP+博弈论 [题解]其实它都不是博弈题…… 很自然的可以设f[i][j]表示i~j先手可取得的最大价值. 容易得到转移式:f[i][j]=max(a[i]+sum[i+1~j]-f[i+ ...
- Mac 上真机调试cocos2d-x-3.16的test程序
文章比较长,一个算是新手又不是新手的程序员的解决过程. 一 xcode中打开项目 首先,下载完成cocos2d-x-3.16之后,解压,然后在根目录build目录下双击cocos2d_tests.xc ...
- YII 框架查询
基础查询 Customer::find()->one(); 此方法返回一条数据: Customer::find()->all(); 此方法返回所有数据: Customer::f ...
- Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- HDU 1026 Ignatius and the Princess I (广搜)
题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...