Codeforces 581F Zublicanes and Mumocrates 树形dp
dp[ i ][ j ][ k ] 表示 以 i 为根的子树, 占领 i 的 是 j 并且第一个人占了 i 子树的 k 个叶子节点的最小值。
然后随便d 1 d 就好了。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, leaf;
int dp[N][][N];
int tmp[][N];
int sum[N];
bool isleaf[N];
vector<int> G[N]; void dfs(int u, int fa) {
if(isleaf[u]) {
dp[u][][] = dp[u][][] = ;
sum[u] = ;
return;
}
dp[u][][] = dp[u][][] = ;
for(auto& v : G[u]) {
if(v == fa) continue;
dfs(v, u);
memset(tmp, inf, sizeof(tmp));
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
for(int c1 = ; c1 <= sum[u]; c1++) {
for(int c2 = ; c2 <= sum[v]; c2++) {
chkmin(tmp[i][c1 + c2], dp[u][i][c1] + dp[v][j][c2] + (i != j));
}
}
}
}
sum[u] += sum[v];
memcpy(dp[u], tmp, sizeof(dp[u]));
}
} int main() {
memset(dp, inf, sizeof(dp));
scanf("%d", &n);
for(int i = ; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
if(n == ) return puts(""), ;
int root = -;
for(int i = ; i <= n; i++) {
if(SZ(G[i]) != ) root = i;
else leaf++, isleaf[i] = true;
}
dfs(root, );
printf("%d\n", min(dp[root][][leaf >> ], dp[root][][leaf >> ]));
return ;
} /*
*/
Codeforces 581F Zublicanes and Mumocrates 树形dp的更多相关文章
- Codeforces 581F Zublicanes and Mumocrates - 树形动态规划
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...
- Codeforces 581F Zublicanes and Mumocrates(树形DP)
题目大概说有一棵树要给结点染色0或1,要求所有度为1的结点一半是0一半是1,然后问怎么染色,使两端点颜色不一样的边最少. dp[0/1][u][x]表示以u结点为根的子树中u结点是0/1色 且其子树有 ...
- Codeforces 581F Zublicanes and Mumocrates(树型DP)
题目链接 Round 322 Problem F 题意 给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- codeforces 337D Book of Evil (树形dp)
题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...
- Codeforces 1276D - Tree Elimination(树形 dp)
Codeforces 题面传送门 & 洛谷题面传送门 繁琐的简单树形 dp(大雾),要是现场肯定弃了去做 F 题 做了我一中午,写篇题解纪念下. 提供一种不太一样的思路. 首先碰到这样的题肯定 ...
- Codeforces 543D Road Improvement(树形DP + 乘法逆元)
题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...
- Codeforces 815C Karen and Supermarket 树形dp
Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...
- Codeforces 627D Preorder Test(二分+树形DP)
题意:给出一棵无根树,每个节点有一个权值,现在要让dfs序的前k个结点的最小值最大,求出这个值. 考虑二分答案,把>=答案的点标记为1,<答案的点标记为0,现在的任务时使得dfs序的前k个 ...
随机推荐
- qt 视频播放器错误解决方法
DirectShowPlayerService::doRender: Unresolved error code 0x80040266 () 当你发布的qmlproject包含QtMultimedia ...
- QPS从0到4000请求每秒,谈达达后台架构演化之路
达达是全国领先的最后三公里物流配送平台. 达达的业务模式与滴滴以及Uber很相似,以众包的方式利用社会闲散人力资源,解决O2O最后三公里即时性配送难题(目前达达已经与京东到家合并). 达达业务主要包含 ...
- luogu 2157 状压dp
f[i][j][k]分别代表1-i-1个人全部打完饭时i及其后7个人的状态为j时最后一个打饭的人为i+k的状态下所用的最小时间 当i已经打过饭时 即 j&1 那么 f [i] [j>&g ...
- Linux ip配置
ifconfig 查看ip ifconfig eth0 192.168.100.10 netmask 255.255.255.0 或者 ifconfig eth0 192.168.100.1 ...
- Linux_解决启动网卡失败 Device eth0 does not seem to be present
Linux_解决启动网卡失败 Device eth0 does not seem to be present 虚拟机克隆 发现service network restart 启动失败 故障现象: ...
- UOJ #460 新年的拯救计划
清真的构造题 UOJ# 460 题意 求将$ n$个点的完全图划分成最多的生成树的数量,并输出一种构造方案 题解 首先一棵生成树有$ n-1$条边,而原完全图只有$\frac{n·(n-1)}{2}$ ...
- Lombok插件看法浅谈
背景 最近接触的几个工程中Lombok插件出现频率比较高,趁机了解一下原理. 简要说明: 受益于JSR 269 API,程序可以在编译阶段对AST进行节点的操作,从而注入相关的功能结点,从而包含在最终 ...
- cout,cerr和clog的区别
官方解释: cout——Standard output stream Object of class ostream that represents the standard output strea ...
- git本机服务器配置(二):TortoiseGit的安装
1. 下载TortoiseGit https://tortoisegit.org/ 1.1 下载安装包 下载对应安装包,如果不知道自己该下载哪个,那就下载32位的. 1.2 下载语言包 下载和安装包相 ...
- CF1153C Serval and Parenthesis Sequence
题目地址:CF1153C Serval and Parenthesis Sequence 思路:贪心 如果有解,那么 \(s_0 = (\) && \(s_{n-1} = )\) &a ...