Codeforces 440 D. Berland Federalization 树形DP,记录DP
题目链接:http://codeforces.com/contest/440/problem/D
Recently, Berland faces federalization requests more and more often. The proponents propose to divide the country into separate states. Moreover, they demand that there is a state which includes exactly k towns.
Currently, Berland has n towns, some pairs of them are connected by bilateral roads. Berland has only n - 1 roads. You can reach any city from the capital, that is, the road network forms a tree.
The Ministry of Roads fears that after the reform those roads that will connect the towns of different states will bring a lot of trouble.
Your task is to come up with a plan to divide the country into states such that:
- each state is connected, i.e. for each state it is possible to get from any town to any other using its roads (that is, the roads that connect the state towns),
- there is a state that consisted of exactly k cities,
- the number of roads that connect different states is minimum.
The first line contains integers n, k (1 ≤ k ≤ n ≤ 400). Then follow n - 1 lines, each of them describes a road in Berland. The roads are given as pairs of integers xi, yi (1 ≤ xi, yi ≤ n; xi ≠ yi) — the numbers of towns connected by the road. Assume that the towns are numbered from 1 to n.
The the first line print the required minimum number of "problem" roads t. Then print a sequence of t integers — their indices in the found division. The roads are numbered starting from 1 in the order they follow in the input. If there are multiple possible solutions, print any of them.
If the solution shows that there are no "problem" roads at all, print a single integer 0 and either leave the second line empty or do not print it at all.
题意:
给你一棵树,最少的删掉哪些边能使得余下的至少有1个大小刚好为k的残树
给出方案
题解:
DP[i][j]表示以i为根删掉j个节点的最少删边数量
V[i][j]表示以i为根删掉j个节点需要删去的边对应的(点u,该u点还需要删去的边数量)
转移较简单
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e2+, M = 1e3+, mod = 1e9+, inf = 2e9; int dp[N][N],sz[N],n,K,a,b,fa[N];
vector<pair<int,int > > G[N];
vector< pair<int ,int > > V[N][N];
void dfs(int u,int f) {
sz[u] = ;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i].first;
if(to == f) continue;
dfs(to,u);
fa[to] = G[u][i].second;
sz[u] += sz[to];
}
}
void dfs2(int u,int f,int index) {
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i].first;
if(to == f) continue;
dfs2(to,u,G[u][i].second);
for(int j = sz[u]; j >= ; --j)
for(int k = ; k <= min(sz[to],j); ++k)
{
if(dp[u][j - k] + dp[to][k] < dp[u][j]) {
dp[u][j] = dp[u][j - k] + dp[to][k];
V[u][j] = V[u][j-k];
V[u][j].push_back(MP(to,k));
}
}
}
if(f) {
dp[u][sz[u]] = ;
V[u][sz[u]].clear();
V[u][sz[u]].push_back(MP(u,sz[u]));
}
else dp[u][sz[u]] = ;
}
int vis[N];
void work(int u,int x) {
vis[u] = ;
if(sz[u] == x) {
cout<<fa[u]<<" ";
return ;
}
for(int i = ; i < V[u][x].size(); ++i) {
work(V[u][x][i].first,V[u][x][i].second);
}
}
int main() {
scanf("%d%d",&n,&K);
for(int i = ; i < n; ++i) {
scanf("%d%d",&a,&b);
G[a].push_back(MP(b,i));
G[b].push_back(MP(a,i));
}
for(int i = ; i <= n; ++i){
for(int j = ; j <= n; ++j) dp[i][j] = inf;
}
dfs(,);
dfs2(,,);
int ans = dp[][n-K],ansj = ;
for(int i = ; i <= n; ++i) {
if(sz[i] >= K && +dp[i][sz[i]-K] < ans) {
ans = min(ans,+dp[i][sz[i]-K]);
ansj = i;
}
}
cout<<ans<<endl;
if(ansj != ) cout<<fa[ansj]<<" ";
work(ansj,sz[ansj] - K);
return ;
}
Codeforces 440 D. Berland Federalization 树形DP,记录DP的更多相关文章
- 蒟蒻的树形dp记录
POJ2342: 题意:某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- 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个 ...
- Codeforces Round #436 (Div. 2) E. Fire(dp 记录路径)
E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- codeforces 416B. Appleman and Tree 树形dp
题目链接 Fill a DP table such as the following bottom-up: DP[v][0] = the number of ways that the subtree ...
- Codeforces 581F Zublicanes and Mumocrates 树形dp
Zublicanes and Mumocrates dp[ i ][ j ][ k ] 表示 以 i 为根的子树, 占领 i 的 是 j 并且第一个人占了 i 子树的 k 个叶子节点的最小值. 然后随 ...
- Codeforces 808G Anthem of Berland【KMP】【DP】
LINK 简要题意 给你一个串s,上面有字母和一些通配符,问你将通配符换成字母之后最多可以出现多少次串t 首先有一个很傻子的做法就是\(dp_{i,j}\)表示s到第i个位置匹配t串前j个字符的完整t ...
- Codeforces Round #338 (Div. 2) C. Running Track dp
C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...
随机推荐
- Java:获取文件内容
文章来源:https://www.cnblogs.com/hello-tl/p/9139353.html import java.io.*; public class FileBasicOperati ...
- ThinkPHP foreach标签
$optionvalue = array( 'MSGTYPE_TEXT'=>'文本消息', 'MSGTYPE_EVENT_SCAN'=>'扫描事件', 'MSGTYPE_EVENT_sub ...
- python基础 : 1.计算机基础 2.注释 3.变量 4.标识符 5.输出 6.格式化输出 7.输入 8.算数运算符 9.字符串操作
- Nowcoder 106 C.Professional Manager(统计并查集的个数)
题意: 给出四种操作: 1. 合并u,v两棵树 2. 从u所在的集合中删除u 3. 询问u所在集合有多少颗树 4. 询问 u,v是否在同一个集合 分析: 对于删除操作, 只要新开一个点代替原来的点即可 ...
- 基于 WPF + Modern UI 的 公司OA小助手 开发总结
前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个阶段的一个反思.人只有在总结的过程中才会发现自己的不足. 公司每天都要在OA系统上上班点击签到,下班点击签退 ...
- 大数据学习——linux常用命令(四)
四 查到命令 1 查找可执行的命令所在的路径 which ls 查ls命令所在的路径 2 查找可执行的命令和帮助的位置 whereiis ls 3 从某个文件夹开始查找文件 find / -name ...
- Scrapy的log日志功能
Logging Scrapy提供了log功能,可以通过 logging 模块使用 可以修改配置文件settings.py,任意位置添加下面两行 LOG_FILE = "mySpider.lo ...
- vs2010 相对路径
相对路径是针对后缀为vcxproj文件而言的. 在VS的工程中常常要设置头文件的包含路径,当然你可以使用绝对路径,但是如果你这样设置了你只能在你自己的机器上运行该工程:如果其他人拷贝你的工程到其他机器 ...
- 给你两个字符串str1,str2,找出str2在str1中的位置
如题 题目参考链接: http://blog.csdn.net/hxz_qlh/article/details/14110221 代码来自非原创 #include <iostream> # ...
- 谈谈APP架构选型:React Native还是HBuilder
原文链接 导读:最近公司的一款新产品APP要进行研发,老大的意思想用H5来做混合APP以达到高效敏捷开发的目的.我自然就开始进行各种技术选型的调研,这里重点想说的是我最后挑选出的2款hybrid ap ...