[Codeforces440D]Berland Federalization
Problem
给你一棵树,最少删掉哪些边,能使得余下的至少有1个大小刚好为k的残树。
1 ≤ k ≤ n ≤ 400
Solution
用f[i][j]表示以i为根有j个节点的最少删边数量
因为此题要输出删除的边
v[i][j]表示以i为根删掉j个节点需要删去的边对应的(点u,该u点还需要删去的边数量)
Notice
因为要输出方案,所以比较复杂
Code
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
typedef pair<int, int> Node;
const int INF = 1e9, N = 400;
const double eps = 1e-6, phi = acos(-1.0);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
struct node
{
int vet, next;
}edge[N + 5];
int head[N + 5], num = 0, f[N + 5][N + 5], Flag[N + 5], fa[N + 5], n, k;
vector<Node> v[N + 5][N + 5];
void add(int u, int v)
{
edge[++num].vet = v;
edge[num].next = head[u];
head[u] = num;
}
void dfs(int u)
{
rep(i, 0, k) f[u][i] = INF;
f[u][1] = 0;
travel(i, u)
{
int vv = edge[i].vet;
if (vv == fa[u]) continue;
fa[vv] = u;
dfs(vv);
per(j, k, 0)
{
f[u][j]++;
rep(t, 0, j)
if (f[u][j - t] + f[vv][t] < f[u][j])
{
f[u][j] = f[u][j - t] + f[vv][t];
v[u][j].clear();
v[u][j].assign(v[u][j - t].begin(), v[u][j - t].end());
v[u][j].push_back(make_pair(vv, t));
}
}
}
}
void Out(int u, int now)
{
Flag[u] = 1;
for (auto i : v[u][now]) Out(i.first, i.second);
travel(i, u)
if (!Flag[edge[i].vet]) printf("%d ", (i + 1) / 2);
}
int sqz()
{
n = read(), k = read();
rep(i, 1, n) head[i] = 0;
rep(i, 1, n - 1)
{
int u = read(), v = read();
add(u, v), add(v, u);
}
dfs(1);
int ans = f[1][k], root = 1;
rep(i, 2, n)
if (f[i][k] + 1 < ans) ans = f[i][k] + 1, root = i;
printf("%d\n", ans) ;
if (ans) Out(root, k);
puts("");
return 0;
}
[Codeforces440D]Berland Federalization的更多相关文章
- [CodeForces-440D]Berland Federalization
题目大意: 给你一棵树,你可以删掉一些边,使得分除去的子树中至少有一棵大小为k. 问最少删去多少边,以及删边的具体方案. 思路: 树形DP. f[i][j]表示以i为根,子树中去掉j个点最少要删边的数 ...
- Codeforces 440 D. Berland Federalization 树形DP,记录DP
题目链接:http://codeforces.com/contest/440/problem/D D. Berland Federalization Recently, Berland faces ...
- cf723d Lakes in Berland
The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cel ...
- CF723D. Lakes in Berland[DFS floodfill]
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 723D: Lakes in Berland
Description The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × ...
- CF 370B Berland Bingo
题目链接: 传送门 Berland Bingo time limit per test:1 second memory limit per test:256 megabytes Descrip ...
- Codeforces Round #Pi (Div. 2) B. Berland National Library set
B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland dfs
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- HDU 3033 分组背包(至少选一个)
分组背包(至少选一个) 我真的搞不懂为什么,所以现在就只能当作是模板来用吧 如果有大牛看见 希望评论告诉我 &代码: #include <cstdio> #include < ...
- 025-缓存Cache
如果每次进入页面的时候都查询数据库生成页面内容的话,如果访问量非常大,则网站性能会非常差.而如果只有第一次访问的时候才查询数据库生成页面内容,以后都直接输出内容,则能提高系统性能.这样无论有多少人访问 ...
- Kubernetes 网络改进的三项实践分享
自研CNI IPAM插件 解决K8s功能问题 首先,在功能方面,Kubernetes 网络模型由于IP不固定,无法对IP资源进行精细管控,无法使用基于IP的监控和基于IP的安全策略,此外,一些IP发现 ...
- centos7安装redist 以及redis扩展
wget http://download.redis.io/releases/redis-3.2.1.tar.gz 用wget下载 $ tar xzf redis-3.2.1.tar.gz 解 ...
- Six advantages of Nissan consult 3 diagnostic tool
Today autonumen.com introduces Nissan consult 3. Nissan Consult 3 is a professional diagnostic tool ...
- 常用git命令总结 初始化git库操作 git 子模块
查看 git status 查看状态 Gitk 界面各个版本查看 添加 Git add filename 添加指定文件 Git add . 操作未暂存的文件 Git add -A 操作所有文件 包括删 ...
- 常用python的标准库
1.itsdangerous # 加密签名的数据 2.re # 正则表达式 3.time # 时间模块 4.keyword # 查看关键字5.random # 随机6.uuid
- js获取本月最后一天
function getLastDay() { var seperator1 = "-"; var date=new Date; var new_mo ...
- JavaScript 字典
JavaScript 字典 字典以 key value 形式出现 使用: a = {'k1':'v1,''k2':'v2'} 获取值: a['k1'] 获取值:v1
- JavaScript 序列化、转义
JavaScript 序列化.转义 序列化 // 将对象转换为字符串 JSON.stringify() // 将字符串转换为对象类型 JSON.parse() 转义 // URl中未转义的字符 de ...