题目链接:http://codeforces.com/contest/440/problem/D

D. Berland Federalization
 

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.
Input

The first line contains integers nk (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 ≤ nxi ≠ yi) — the numbers of towns connected by the road. Assume that the towns are numbered from 1 to n.

Output

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

  1. 蒟蒻的树形dp记录

    POJ2342: 题意:某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多 ...

  2. codeforces 161D Distance in Tree 树形dp

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

  3. Codeforces 815C Karen and Supermarket 树形dp

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

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

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

  5. 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 ...

  6. 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 ...

  7. Codeforces 581F Zublicanes and Mumocrates 树形dp

    Zublicanes and Mumocrates dp[ i ][ j ][ k ] 表示 以 i 为根的子树, 占领 i 的 是 j 并且第一个人占了 i 子树的 k 个叶子节点的最小值. 然后随 ...

  8. Codeforces 808G Anthem of Berland【KMP】【DP】

    LINK 简要题意 给你一个串s,上面有字母和一些通配符,问你将通配符换成字母之后最多可以出现多少次串t 首先有一个很傻子的做法就是\(dp_{i,j}\)表示s到第i个位置匹配t串前j个字符的完整t ...

  9. 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 ...

随机推荐

  1. Linux 配置yum本地安装源

    cat /etc/system-release Red Hat Enterprise Linux Server release 7.0 (Maipo) 1 使用本地源得先挂载ISO光盘文件 # 可以创 ...

  2. ssm+activiti+maven

    1spring整合activiti中添加activiti依赖 <!-- 添加Activiti支持 --> <dependency> <groupId>org.act ...

  3. Spring核心技术(十)——JSR-330标准注解

    从Spring 3.0开始,Spring开始支持JSR-330标准的注解(依赖注入).这些注解和Spring注解扫描的方式是一直的,开发者只需要在classpath中配置相关的jar包即可. 如果开发 ...

  4. 如何在小程序实现图片lazy-load懒加载效果

    自从跳一跳出现之后小程序又开始频繁出现了,在学习过程中发现小程序虽然好但是由于api不完善导致开发过程中有很多的坑,重点是网上相对小程序出现坑时解决方案显然比较少,小程序最让人觉得痛心疾首之一就是无法 ...

  5. 用python做分布式定时器

    分布式任务系统 (Python) github地址 https://github.com/thomashuang/Lilac/blob/master/README.rst 这里将介绍Liac的设计架构 ...

  6. Python 双向队列Deque、单向队列Queue 模块使用详解

    Python 双向队列Deque 模块使用详解 创建双向队列Deque序列 双向队列Deque提供了类似list的操作方法: #!/usr/bin/python3 import collections ...

  7. Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现

     Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现 LayerDrawable实现的结果和附录文章1,2,3中的layer-list一致. ...

  8. POJ 3667 线段树的区间合并简单问题

    题目大意:有一排标号1-N的房间.操作一:询问是不是有连续长度为a的空房间,有的话住进最左边(占用a个房间)操作二:将[a,a+b-1]的房间清空(腾出b个房间)思路:记录每个区间中“靠左”“靠右”“ ...

  9. 【树状数组+dp】HDU 5542 The Battle of Chibi

    http://acm.hdu.edu.cn/showproblem.php?pid=5542 [题意] 给定长为n的序列,问有多少个长为m的严格上升子序列? [思路] dp[i][j]表示以a[i]结 ...

  10. xhprof安装&&使用[转载]

    编译安装 wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar zxf xhprof-0.9.2.tgz cd xhprof-0.9.2/extensio ...