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 ...
随机推荐
- 基于selenium爬取拉勾网职位信息
1.selenium Selenium 本是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.而这一特性为爬虫开发提供了一个选择及方向,由于其本身依赖 ...
- ArrayList练习之存储自定义对象并遍历
新建一个Student.java类 Student.java /* * 这是一个学生类 */ public class Student { private String name; private i ...
- 让Selenium稳定运行的技巧
Selenium简介 Selenium是非常流行的Web自动化测试工具.它具有自动化测试用例制作简单,支持多种浏览器和不同的操作系统等优点. Selenium脚本不稳定的问题 有很多时候Seleniu ...
- NYOJ 239 月老的难题
月老的难题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 月老准备给n个女孩与n个男孩牵红线,成就一对对美好的姻缘. 现在,由于一些原因,部分男孩与女孩可能结成幸福 ...
- 洛谷P1521 求逆序对 题解
题意: 求1到n的全排列中有m对逆序对的方案数. 思路: 1.f[i][j]表示1到i的全排列中有j对逆序对的方案数. 2.显然,1到i的全排列最多有(i-1)*i/2对逆序对,而对于f[i][j]来 ...
- [luoguP2461] [SDOI2008]递归数列(DP + 矩阵优化)
传送门 本题主要是构造矩阵,我们只需要把那一段式子看成两个前缀和相减, 然后就直接矩阵连乘. 直接对那个k+1阶矩阵快速幂即可,注意初始化矩阵为单位矩阵,即主对角线(左上到右下)都为1其他都为0. 另 ...
- POJ 1904:King's Quest【tarjan】
题目大意:给出一个二分图的完美匹配(王子和公主的烧死名单表),二分图x部和y部均只有n个点,问对于每一个x部的点,他能选择哪些点与之匹配 使得与之匹配后,剩余图的最大匹配仍然是n 思路:这题是大白书3 ...
- 【2018 Multi-University Training Contest 2】
01: 02: 03: 04:https://www.cnblogs.com/myx12345/p/9394511.html 05: 06: 07:https://www.cnblogs.com/my ...
- POJ3177,/3352.求最少添加多少边使无向图边双连通
俩个题一样.tarjan算法应用,开始求桥,WA,同一个边双连通分量中low值未必都相同,不能用此来缩点.后来用并查集来判断,若不是桥,则在一个双连通分量中,并之,后边再查,将同一个双连通分量中的点通 ...
- codechef FUN WITH TREES
题目大意: 给一棵树root=1的树: 给一些操作:u v 的路径所有节点的node + val: 最后m个询问:u 节点(包括u) sum%mod 是多少. LCA + RMQ: 我们每次mark ...