题目链接: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. CSS3---媒体查询与响应式布局

    1. 值 设备类型 All 所有设备 Braille 盲人用点字法触觉回馈设备 Embossed 盲文打印机 Handheld 便携设备 Print 打印用纸或打印预览视图 Projection 各种 ...

  2. 剑指Offer(书):斐波那契数列

    题目:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 分析:第一种方法:递归,45时,时间为5s,50时,我就等不及了.原因是重 ...

  3. 7-16 Sort with Swap(0, i)(25 分)

    7-16 Sort with Swap(0, i)(25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy ...

  4. Android开发——后台获取用户点击位置坐标(可获取用户支付宝密码)

    1. getevent命令 我们首先是根据adb shell getevent命令获取到被点击位置的信息. 这里要说明的是,不同的手机手机获得的点击输出是不一样的.以我的真机为例,输出如下 本文原创, ...

  5. SHELL十三问[转载自CU论坛]

    原文地址:http://bbs.chinaunix.net/thread-218853-1-1.html 一.为什么称作shell? http://bbs.chinaunix.net/viewthr ...

  6. 【转】反向AJAX

    原文链接:http://blog.csdn.net/lccone/article/details/7743886 反向Ajax的基本概念是客户端不必从服务器获取信息,服务器会把相关信息直接推送到客户端 ...

  7. python学习笔记--python简介

    一.什么是python? python是一种面向对象.解释型的高级程序语言.python具有语法简洁.易于学习.功能强大,可扩展性强,跨平台等诸多特点.1989年开始开发,于1991年发布第一个公开发 ...

  8. POJ 1017 Packet

    http://poj.org/problem?id=1017 有1*1 2*2...6*6的物品 要装在 6*6的parcel中 问最少用多少个parcel 一直没有找到贪心的策略 问题应该出现在 总 ...

  9. lightoj 1293 - Document Analyzer [ 两指针 + 字符串 ]

    传送门 1293 - Document Analyzer   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: ...

  10. 全新开始fighting

    a.python准备工作 Python种类: JPython   IronPython    JavaScriptPython   RubyPython   CPython    ********** ...