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个 ...
随机推荐
- C#使用WindowsMediaPlayer实现视频播放
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- Harbor删除镜像后且GC清理后,磁盘空间没有释放的问题
1.原因 Harbor删除镜像后且GC清理后,磁盘空间没有释放.因为我们push大量相同标签的镜像,Docker 镜像由标签引用,并由唯一的摘要标识.这意味着如果myImage使用标记推送两个图像,在 ...
- ActiveMQ 的安装与使用(单节点)
环境:CentOS6.6.JDK8 1.下载:http://archive.apache.org/dist/activemq/5.11.1/apache-activemq-5.11.1-bin.tar ...
- CSS3总结一:border(边框)
Border-CSS1的属性 Border-CSS1:border Border-CSS1:border-style Border-CSS1:border-width Border-CSS1:bord ...
- PMP知识点(一)——风险登记册
一.Reference: [管理心得之四十八]<风险登记册>本身的风险 问题日志与风险登记册的区别与联系 PMBOK重要概念梳理之二十六 风险登记册 风险登记单-MBAlib 二.Atta ...
- S2-045漏洞初步分析
0x01 前言 前几天刚分析完s2-032这个漏洞,今天又爆发了一个s2-045的漏洞,又是直接的命令执行,影响了struts2绝大多数的版本. 官方给的漏洞公告在这里 https://cwiki ...
- 程序到CPU的路径
汇编 源码->编译->CPU C/C++ 源码->编译->机器码->系统(执行)->CPU Java/.NET 源码->编译->J字节码->虚拟机 ...
- 背景上实现阴影——linear-gradient
/*从元素顶部有条阴影,两种方式,第二种更好,能控制阴影的宽度*/background-image: linear-gradient(0deg, rgba(226, 226, 226, 0) 97%, ...
- 这是一个蒟蒻的计划……QAQ
感觉像我这种拖拉的人很有可能是完成不了的,挂上来相当于监督我自己啦QWQ [学习计划] [√]1.去看Trie树!!! yyb学长的blog 2.KMP还有AC自动机 先贴两个链接在这里吧:KMP ...
- js reduce()方法使用
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. 注意: reduce() 对 ...