Codeforces 581F Zublicanes and Mumocrates - 树形动态规划
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on n main squares of the capital of Berland. Each of the n squares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties.
Some pairs of squares are connected by (n - 1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares.
The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even.
To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.
The first line of the input contains a single integer n (2 ≤ n ≤ 5000) — the number of squares in the capital of Berland.
Next n - 1 lines contain the pairs of integers x, y (1 ≤ x, y ≤ n, x ≠ y) — the numbers of the squares connected by the road. All squares are numbered with integers from 1 to n. It is guaranteed that the number of dead end squares of the city is even.
Print a single number — the minimum number of roads connecting the squares with demonstrations of different parties.
8
1 4
2 4
3 4
6 5
7 5
8 5
4 5
1
5
1 2
1 3
1 4
1 5
2
题目大意
给定一棵有$n$个点的无根树(度为1的点不为根),保证它的叶节点的个数为偶数。将所有点染成黑白两种颜色,要求
- 黑的叶节点数等于白的叶节点数
- 有边相连但颜色不同的点对数最少
问最少的这样的点对数。
显然动态规划。
Solution 1
用$f[i][j][0/1]$表示当前考虑$i$号点,它的子树内有$j$个叶节点是黑色的最优结果。
转移是显然的。
至于时间复杂度为什么可过?下面解释一下(为了方便计算,那么就用子树$size$来说明吧)
设当前考虑的节点的第$i$个子节点为$s_{i}$。
$\sum_{i = 1}size[s_{i}]\cdot\sum_{j = 1} ^ {i - 1}size[s_{j}] = \sum_{i < j}size[s_{i}]\cdot size[s_{j}]$
然后可以发现对于任意一对节点$\left(u, v\right)$仅对它们的lca有1的贡献,所以总时间复杂度为$O\left(n^{2}\right)$
Code
/**
* Codeforces
* Problem#581F
* Accepted
* Time: 139ms
* Memory: 198380k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define smin(_a, _b) (_a = min(_a, _b)) const int N = ; int n;
vector<int> *g;
// 0: black, 1: white
int f[N][N][]; // node, number of the black nodes, the color of this node
int temp[N][];
int root;
int clf[N];
int deg[N]; inline void init() {
scanf("%d", &n);
g = new vector<int>[(n + )];
for(int i = , u, v; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
deg[u]++, deg[v]++;
}
for(root = ; root < n && deg[root] == ; root++);
} void treedp(int node, int fa) {
if(deg[node] == ) {
f[node][][] = f[node][][] = ;
clf[node] = ;
return;
}
// memset(temp)
clf[node] = ;
f[node][][] = f[node][][] = ;
for (int i = ; i < deg[node]; i++) {
int e = g[node][i];
if (e == fa) continue;
treedp(e, node);
memset(temp, 0x3f, sizeof(temp));
for (int s1 = clf[node]; ~s1; s1--) {
for (int s2 = clf[e]; ~s2; s2--) {
smin(temp[s1 + s2][], f[node][s1][] + min(f[e][s2][], f[e][s2][] + ));
smin(temp[s1 + s2][], f[node][s1][] + min(f[e][s2][] + , f[e][s2][]));
}
}
clf[node] += clf[e];
for (int j = ; j <= clf[node]; j++)
f[node][j][] = temp[j][], f[node][j][] = temp[j][];
}
} inline void solve() {
memset(f, 0x3f, sizeof(f));
treedp(root, );
int k = clf[root] >> ;
int ans = min(f[root][k][], f[root][k][]);
printf("%d\n", ans);
} int main() {
init();
solve();
return ;
}
Slower Version
Solution 2
由于转移的时候仅和当前节点的颜色和它的父节点的颜色是否相同有关,所以用$f[i][j]$表示当前考虑第$i$号点,它的子树内有$j$个叶节点是黑色的最优结果。
怎么转移呢?
先当父节点颜色和当前节点颜色相同,按照上面的方法进行转移。
然后考虑将当前子树内的所有点的颜色反转,这样会导致当前点和父节点的颜色不同,答案加1,这样去更新。
Code
/**
* Codeforces
* Problem#581F
* Accepted
* Time: 61ms
* Memory: 100280k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define smin(_a, _b) (_a = min(_a, _b)) const int N = ; int n;
vector<int> g[N];
int f[N][N]; // node, number of the black nodes
int root;
int clf[N];
int deg[N]; inline void init() {
scanf("%d", &n);
for(int i = , u, v; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
deg[u]++, deg[v]++;
}
for(root = ; root < n && deg[root] == ; root++);
} void treedp(int node, int fa) {
if(deg[node] == ) {
f[node][] = , f[node][] = ;
clf[node] = ;
return;
}
clf[node] = ;
f[node][] = ;
for (int i = ; i < deg[node]; i++) {
int e = g[node][i];
if (e == fa) continue;
treedp(e, node);
for (int s1 = clf[node]; ~s1; s1--) {
for (int s2 = clf[e]; ~s2; s2--) {
smin(f[node][s1 + s2], f[node][s1] + f[e][s2]);
}
}
clf[node] += clf[e];
}
for (int i = ; i <= clf[node]; i++)
smin(f[node][i], f[node][clf[node] - i] + ); // reverse the color of each node
} inline void solve() {
memset(f, 0x3f, sizeof(f));
treedp(root, );
int k = clf[root] >> ;
printf("%d\n", f[root][k]);
} int main() {
init();
solve();
return ;
}
Codeforces 581F Zublicanes and Mumocrates - 树形动态规划的更多相关文章
- Codeforces 581F Zublicanes and Mumocrates 树形dp
Zublicanes and Mumocrates dp[ i ][ j ][ k ] 表示 以 i 为根的子树, 占领 i 的 是 j 并且第一个人占了 i 子树的 k 个叶子节点的最小值. 然后随 ...
- 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$的结点),现在要把所有的点染色(黑或白) 要求一半叶子结点的颜色为白,一半叶子结点的 ...
- 树形dp - Codeforces Round #322 (Div. 2) F Zublicanes and Mumocrates
Zublicanes and Mumocrates Problem's Link Mean: 给定一个无向图,需要把这个图分成两部分,使得两部分中边数为1的结点数量相等,最少需要去掉多少条边. ana ...
- Codeforces Round #322 (Div. 2) —— F. Zublicanes and Mumocrates
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...
- Codeforces 835F Roads in the Kingdom - 动态规划
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...
- 蓝桥杯 ALGO-4 结点选择 (树形动态规划)
问题描述 有一棵 n 个节点的树,树上每个节点都有一个正整数权值.如果一个点被选择了,那么在树上和它相邻的点都不能被选择.求选出的点的权值和最大是多少? 输入格式 第一行包含一个整数 n . 接下来的 ...
- 树形动态规划(树状DP)小结
树状动态规划定义 之所以这样命名树规,是因为树形DP的这一特殊性:没有环,dfs是不会重复,而且具有明显而又严格的层数关系.利用这一特性,我们可以很清晰地根据题目写出一个在树(型结构)上的记忆化搜索的 ...
- Codeforces 1000G Two-Paths 树形动态规划 LCA
原文链接https://www.cnblogs.com/zhouzhendong/p/9246484.html 题目传送门 - Codeforces 1000G Two-Paths 题意 给定一棵有 ...
随机推荐
- C#通过RFC连接sap系统
先理解一下 RFC(Romote Function Call)远程函数调用 调用前提: 1.要想通过C# 通过RFC调用SAP端,SAP端要存在RFC远程调用的函数才行(例如SAP端通过SE37创建) ...
- MySQL.配置MariaDB的字符集
配置MariaDB的字符集 环境: 操作系统:CentOS Linux release 7.x mariadb安装及配置 yum install mariadb-server mariadb #安装 ...
- 百度编辑器 Ueditor
针对与编辑器里面的图片的存储问题:\ueditor\1.4.3\php\conf.json 文件里面 /* 前后端通信相关的配置,注释只允许使用多行方式 */{/* 上传图片配置项 */“imageA ...
- jQuery获取点击对象的父级
一.使用$('body').on('click','.index',function(event){})绑定事件时,例: <div class="project-box"&g ...
- case when 遇到varchar转为int类型值失败的错误
问题描述: 在Sql Server 2005下, 使用如下语句报错:在将 varchar 值 '大' 转换成数据类型 int 时失败. 注:status 是整型字段 select ff= case ...
- Android Studio -- 关联源码
1,昨天刚把SDK升级到25,然后准备开始 新的一年码代码,结果发现查看源码的时候出现了一堆的“ throw new RuntimeException("Stub!");” 网上搜 ...
- 【转】基于Selenium的web自动化框架(python)
1 什么是selenium Selenium 是一个基于浏览器的自动化工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.Sel ...
- uvalive 3126 Taxi Cab Scheme
题意: 有m个人要坐出租车,每个人给出出发时间,出发地点和目的地(以二维坐标表示),两个地点之间所花的时间计算方式是两点之间的哈密顿距离.现在需要排遣车出去,一辆车每次只能装一个人,如果一辆车在装完一 ...
- Linux账号管理
Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...
- 一款用于对 WiFi 接入点安全进行渗透测试的工具
越来越多的设备通过无线传输的方式连接到互联网,以及,大范围可用的 WiFi 接入点为攻击者攻击用户提供了很多机会.通过欺骗用户连接到虚假的 WiFi 接入点,攻击者可以完全控制用户的网络连接,这将使得 ...