题目大意:
  给你一棵树,你可以删掉一些边,使得分除去的子树中至少有一棵大小为k。
  问最少删去多少边,以及删边的具体方案。

思路:
  树形DP。
  f[i][j]表示以i为根,子树中去掉j个点最少要删边的数量;
  v[i][j]表示其具体方案。
  然后对每个点跑一下背包。
  状态转移方程f[x][k+j]=min{f[x][k]+f[y][j]|y为x子结点}。
  注意y的不同状态不能同时对x的同一个状态产生影响,所以转移的时候必须把数组复制一遍,将原数组和转移过后的数组隔离开来。
  v的转移只需要把v[x][k]和v[y][j]并起来就OK了。
  最后答案枚举每个结点的f[node][size[node]-k],如果不是一开始假定的根结点1,我们要将其与主树连接的边算入答案。

 #include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int inf=0x7fffffff;
const int N=;
struct Edge {
int to,id;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const int &i) {
e[u].push_back((Edge){v,i});
e[v].push_back((Edge){u,i});
}
int size[N],f[N][N];
int t[N],pre[N];
std::vector<int> v[N][N];
void dfs(const int &x,const int &par) {
size[x]=;
int eid;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i].to;
if(y==par) {
eid=e[x][i].id;
continue;
}
pre[y]=e[x][i].id;
dfs(y,x);
size[x]+=size[y];
}
f[x][]=;
f[x][size[x]]=;
v[x][size[x]].push_back(eid);
std::fill(&f[x][],&f[x][size[x]],inf);
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i].to;
eid=e[x][i].id;
if(y==par) continue;
memcpy(t,f[x],sizeof t);
for(int j=;j<=size[y];j++) {
for(int k=;k<size[x]-j;k++) {
if(f[x][k]==inf) continue;
if(f[x][k]+f[y][j]<t[k+j]) {
t[k+j]=f[x][k]+f[y][j];
v[x][k+j]=v[x][k];
v[x][k+j].insert(v[x][k+j].end(),v[y][j].begin(),v[y][j].end());
}
}
}
memcpy(f[x],t,sizeof t);
}
}
int main() {
int n=getint(),k=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint(),i);
}
dfs(,);
int ans=f[][size[]-k],node=;
for(register int root=;root<=n;root++) {
if(size[root]<k) continue;
f[root][size[root]-k]++;
v[root][size[root]-k].push_back(pre[root]);
if(f[root][size[root]-k]<ans) {
ans=f[root][size[root]-k];
node=root;
}
}
printf("%d\n",ans);
for(register unsigned i=;i<v[node][size[node]-k].size();i++) {
printf("%d ",v[node][size[node]-k][i]);
}
return ;
}

[CodeForces-440D]Berland Federalization的更多相关文章

  1. Codeforces 440 D. Berland Federalization 树形DP,记录DP

    题目链接:http://codeforces.com/contest/440/problem/D D. Berland Federalization   Recently, Berland faces ...

  2. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  3. CodeForces 567B Berland National Library

    Description Berland National Library has recently been built in the capital of Berland. In addition, ...

  4. Codeforces B - Berland National Library

    B. Berland National Library time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. AC日记——codeforces Ancient Berland Circus 1c

    1C - Ancient Berland Circus 思路: 求出三角形外接圆: 然后找出三角形三条边在小数意义下的最大公约数; 然后n=pi*2/fgcd; 求出面积即可: 代码: #includ ...

  6. CodeForces - 1073D Berland Fair

    XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnbooths, arranged in ...

  7. Codeforces 1296F Berland Beauty

    题目链接:http://codeforces.com/problemset/problem/1296/F 思路: 1————2————3————4————5————6 1->3 2 2-> ...

  8. CodeForces 567B Berland National Library hdu-5477 A Sweet Journey

    这类题一个操作增加多少,一个操作减少多少,求最少刚开始为多少,在中途不会出现负值,模拟一遍,用一个数记下最大的即可 #include<cstdio> #include<cstring ...

  9. [Codeforces440D]Berland Federalization

    Problem 给你一棵树,最少删掉哪些边,能使得余下的至少有1个大小刚好为k的残树. 1 ≤ k ≤ n ≤ 400 Solution 用f[i][j]表示以i为根有j个节点的最少删边数量 因为此题 ...

随机推荐

  1. 【leetcode 简单】第三题 回文数

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  2. hihoCoder #1175 : 拓扑排序·二

    题目链接:http://hihocoder.com/problemset/problem/1175 代码实现如下: #include <queue> #include <cstdio ...

  3. 模型稳定度指标PSI与IV

    由于模型是以特定时期的样本所开发的,此模型是否适用于开发样本之外的族群,必须经过稳定性测试才能得知.稳定度指标(population stability index ,PSI)可衡量测试样本及模型开发 ...

  4. Coursera在线学习---第六节.构建机器学习系统

    备: High bias(高偏差) 模型会欠拟合    High variance(高方差) 模型会过拟合 正则化参数λ过大造成高偏差,λ过小造成高方差 一.利用训练好的模型做数据预测时,如果效果不好 ...

  5. python基础===如何在列表,字典,集合中根据条件筛选数据

    #常见的操作如下: data = [1, 5, -3, -2, 6, 0, 9] res = [] for x in data: if x>=0: res.append(x) print(res ...

  6. Deep Learning基础--各个损失函数的总结与比较

    损失函数(loss function)是用来估量你模型的预测值f(x)与真实值Y的不一致程度,它是一个非负实值函数,通常使用L(Y, f(x))来表示,损失函数越小,模型的鲁棒性就越好.损失函数是经验 ...

  7. 读书笔记 effective c++ Item 3 在任何可能的时候使用 const

    Const可以修饰什么?   Const 关键字是万能的,在类外部,你可以用它修饰全局的或者命名空间范围内的常量,也可以用它来修饰文件,函数和块作用域的静态常量.在类内部,你可以使用它来声明静态或者非 ...

  8. Codefroces 735D Taxes(哥德巴赫猜想)

    题目链接:http://codeforces.com/problemset/problem/735/D 题目大意:给一个n,n可以被分解成n1+n2+n3+....nk(1=<k<=n). ...

  9. C++的黑科技(深入探索C++对象模型)

    周二面了腾讯,之前只投了TST内推,貌似就是TST面试了 其中有一个问题,“如何产生一个不能被继承的类”,这道题我反反复复只想到,将父类的构造函数私有,让子类不能调用,最后归结出一个单例模式,但面试官 ...

  10. 铁器 · Burp Suite

    Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程.所有的工具都共享一个能处理并显示HTTP 消息,持久性,认证,代 ...