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的更多相关文章

  1. [CodeForces-440D]Berland Federalization

    题目大意: 给你一棵树,你可以删掉一些边,使得分除去的子树中至少有一棵大小为k. 问最少删去多少边,以及删边的具体方案. 思路: 树形DP. f[i][j]表示以i为根,子树中去掉j个点最少要删边的数 ...

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

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

  3. 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 ...

  4. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 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 × ...

  6. CF 370B Berland Bingo

    题目链接: 传送门 Berland Bingo time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 论文阅读(XiangBai——【AAAI2017】TextBoxes_A Fast Text Detector with a Single Deep Neural Network)

    XiangBai——[AAAI2017]TextBoxes:A Fast Text Detector with a Single Deep Neural Network 目录 作者和相关链接 方法概括 ...

  2. Linux基础培训知识点汇总

    一.Linux简介1.Linux操作系统诞生于1991年10月5日,由林纳斯·托瓦兹在comp.os.minix新闻组上发布消息,正式向外宣布Linux内核的诞生.2.Linux同时也衍生了很多版本( ...

  3. WebForm母版页

    母版页:可以把界面的部分代码进行重用 添加新项-母版页 在母版页中界面代码不要写在 <asp:ContentPlaceHolder ID="head" runat=" ...

  4. Jmeter对SQL Server进行压力测试

    下载地址 https://mirrors.tuna.tsinghua.edu.cn/apache//jmeter/binaries/ 选择binary版本即可 下载的是apache-jmeter-5. ...

  5. js 图片无缝滚动

    html部分 <div id="roll"> <a href="javascript:void(0)" class="prev&qu ...

  6. jquery首页图片轮播

    css样式 .bannerBox {position: relative;width: 100%;height: 348px;margin:0px auto;}.bannerBox .bannerLi ...

  7. Html 标签种类

    Html 标签种类 自闭合标签与闭合标签 自闭合标签 <标签 /> 闭合标签 <标签></标签> 块级标签与行内标签 浏览器审查元素使用: (1) f12 查看每个 ...

  8. mysql的并发控制

    并发即指在同一时刻,多个操作并行执行.MySQL对并发的处理主要应用了两种机制——是"锁"和"多版本控制". 1.并发控制 MySQL提供两个级别的并发控制:服 ...

  9. 剑指offer(18)二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  10. 继上篇后的Excel批量数据导入

    上篇Excel动态生成模板,此篇将借用此模板进行Excel数据的批量导入. 说明:由于数据库中部分数据储存的是编码或者Id,因此,这里就需要用到上篇中的全局数据,判断是否有数据,有数据直接使用,没有数 ...