Zublicanes and Mumocrates

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的更多相关文章

  1. Codeforces 581F Zublicanes and Mumocrates - 树形动态规划

    It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...

  2. Codeforces 581F Zublicanes and Mumocrates(树形DP)

    题目大概说有一棵树要给结点染色0或1,要求所有度为1的结点一半是0一半是1,然后问怎么染色,使两端点颜色不一样的边最少. dp[0/1][u][x]表示以u结点为根的子树中u结点是0/1色 且其子树有 ...

  3. Codeforces 581F Zublicanes and Mumocrates(树型DP)

    题目链接  Round 322 Problem F 题意  给定一棵树,保证叶子结点个数为$2$(也就是度数为$1$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...

  4. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  5. codeforces 337D Book of Evil (树形dp)

    题目链接:http://codeforces.com/problemset/problem/337/D 参考博客:http://www.cnblogs.com/chanme/p/3265913 题目大 ...

  6. Codeforces 1276D - Tree Elimination(树形 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 繁琐的简单树形 dp(大雾),要是现场肯定弃了去做 F 题 做了我一中午,写篇题解纪念下. 提供一种不太一样的思路. 首先碰到这样的题肯定 ...

  7. Codeforces 543D Road Improvement(树形DP + 乘法逆元)

    题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...

  8. Codeforces 815C Karen and Supermarket 树形dp

    Karen and Supermarket 感觉就是很普通的树形dp. dp[ i ][ 0 ][ u ]表示在 i 这棵子树中选择 u 个且 i 不用优惠券的最小花费. dp[ i ][ 1 ][ ...

  9. Codeforces 627D Preorder Test(二分+树形DP)

    题意:给出一棵无根树,每个节点有一个权值,现在要让dfs序的前k个结点的最小值最大,求出这个值. 考虑二分答案,把>=答案的点标记为1,<答案的点标记为0,现在的任务时使得dfs序的前k个 ...

随机推荐

  1. 机器学习中模型泛化能力和过拟合现象(overfitting)的矛盾、以及其主要缓解方法正则化技术原理初探

    1. 偏差与方差 - 机器学习算法泛化性能分析 在一个项目中,我们通过设计和训练得到了一个model,该model的泛化可能很好,也可能不尽如人意,其背后的决定因素是什么呢?或者说我们可以从哪些方面去 ...

  2. springBoot+ vue+ Element-ui实现合并多图上传(一次请求多张图片)

    这次上传使用的是Elemet-ui的uoload上传组件,组件预留的钩子回调还是比较充足的. 1:  实现多图上传主要用到以下两个属性: 下面讲一下属性使用: <el-upload :actio ...

  3. js循环内0.5s停止

    var time_begin = new Date().getTime(); while(true){ if(new Date().getTime()-time_begin > 500) bre ...

  4. navicat连接IEE数据库查询话单

    select * from cdl_raw_1x_12501_ztev8_sht_201811 t1 WHERE ( t1.call_start_time >= STR_TO_DATE( '20 ...

  5. 题解 P1496 【火烧赤壁】

    蒟蒻的第一篇题解,其实这道题是标准的离散化,模拟可以过,但是就没有训练效果了.我们首先先看数据,n<=20000,数据不多,但是范围大(-10^9<=Ai,Bi<=10^9),这时, ...

  6. R-----shiny包的部分解释和控件介绍

    R-----shiny包的部分解释和控件介绍 作者:周彦通.贾慧 shinyApp( ui = fixedPage( fixedPanel( top = 50, right=50, width=200 ...

  7. P3203 [HNOI2010]弹飞绵羊

    LCT裸题,之后填坑打一下 分块做法:每个点存几次出块以及出块的位置,问的时候直接暴力跳就vans了 首先思考最普通的模拟,发现可以O(n)路径压缩,O(1)的查询,但是需要修改就变成了O(n^2)的 ...

  8. JavaScript之iframe页面间通信

    [1] iframe父子页面间通信 1.相互调用对方的方法 |> 子级页面调用父级页面 window.parent.父级页面方法(args) |> 父级页面调用子级页面 document. ...

  9. Spring-Boot配置文件web性能(服务器)配置项

    参数 介绍server.address 服务器应绑定到的网络地址server.compression.enabled = false 如果启用响应压缩server.compression.exclud ...

  10. MFC常用宏

    MFC调试宏 TRACE()   其形式与函数printf()的参数一样,功能是在调试运行时把表达式的值输出到Output调试窗口. Debug版有效 ASSERT()——断言宏,  表达式为真,则程 ...