题目链接: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. alt、title和label

    alt是html标签的属性,而title既是html标签,又是html属性. title标签这个不用多说,网页的标题就是写在<title></title>这对标签之内的. ti ...

  2. Java:清空文件内容

    文章来源:https://www.cnblogs.com/hello-tl/p/9139432.html import java.io.*; public class FileBasicOperati ...

  3. Fiddler使用配置遇到的问题

    针对Fiddler使用遇到的问题记录,方便后期再使用. 1.Chrome导入证书失败,提示"提示由于存储区只读的,存储区已满..." 方法:直接去控制台添加 详细参考:http:/ ...

  4. 跟初学者学习IbatisNet第三篇

    这一章我们主要介绍一下IbatisNet里面的动态sql语句的运用,比如有时候我们想进行模糊查询,参数是动态加入的.或者要实现top n ,order by ,分页等功能的时候,我们就不得不用动态拼接 ...

  5. 【转】阿里巴巴分布式服务框架 Dubbo 团队成员梁飞专访

    原文链接:http://www.iteye.com/magazines/103   Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000+ 个服务提供3,000,000,000+ ...

  6. C#排序1(冒泡排序、直接排序、快速排序)

    冒泡排序:就是两个两个的这个比较好理解,代码也比较好写出来. 它的原理就是相邻的两个两个的比较,如果前面的数比后面的大,那么交换,它这个在比较完一次的时候可以得到最大的一个数,然后接着循环,每次外循环 ...

  7. NYOJ760-See LCS again,有技巧的暴力!

    See LCS again 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 There are A, B two sequences, the number of ele ...

  8. hrbust-1909理工门外的树,不用线段数,贪心思路~~

    理工门外的树 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 605(125 users) Total Accepted: 154(11 ...

  9. Enchantress(hdu 3922)

    首先考虑覆盖三个点的情况,有两种情况: ①:三个点都在圆上,则该圆是三角形的外接圆 ②:两个点在圆上,第三个点在圆内,且在圆上的两个点之间的线段一定是直径 如果是多个圆,就不停地迭代. 有一点重要的是 ...

  10. POJ 3615 Cow Hurdles

    http://poj.org/problem?id=3615 floyd 最短路径的变形 dist[i][j]变化为 : i j之间的最大边 那么输入的时候可以直接把dist[i][j] 当作i j ...