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. 压缩图片 Image

    图片压缩 class resizeImg: """缩略图""" def __init__(self,**args): self.args_k ...

  2. python_study-1

    # Author:larlly'''函数1.在Python交互式命令行下,可以直接输入代码,然后执行,并立刻得到结果.2.文本编辑器推荐俩款 http://www.sublimetext.com/ h ...

  3. flask的简单使用

    一.Flask中的CBV from flask import Flask, render_template from flask import views app = Flask(__name__, ...

  4. P3628 [APIO2010]特别行动队(斜率优化dp)

    P3628 [APIO2010]特别行动队 设$s[i]$为战斗力前缀和 显然我们可以列出方程 $f[i]=f[j]+a*(s[i]-s[j])^{2}+b*(s[i]-s[j])+c$ $f[i]= ...

  5. 剑指offer(37)数字在排序数组中出现的次数。

    题目描述 统计一个数字在排序数组中出现的次数. 题目分析 这题用暴力解也可以过,不过面试官肯定期待更好的解法. 查找我们最熟悉的就是二分查找了,不过二分查找查找的数在数组中只有一个,我们这里却有很多个 ...

  6. CEF 设置Cookie

    参考文档:http://magpcss.org/ceforum/apidocs3/projects/(default)/CefCookieManager.html 转载:https://www.cnb ...

  7. 分析hello1项目里面的web.xml

    在example目录下的web\jsf\hello1\target\hello1\WEB-INF路径里可以找到hello1的web.xml <?xml version="1.0&quo ...

  8. JavaScript自定义求和函数

    我爱撸码,撸码使我感到快乐!大家好,我是Counter,当看到这个标题到时候是不是感觉很简单,千万不要大意哦,你说0.1 + 0.2 = 0.3 ?有时候计算机并不是我们所说绝对精确,这个时候就要我们 ...

  9. Python将list中的string批量转化成int/float

    data = ['] data = map(eval, data) print data 输出:[1, 3.2, 2] 但是在Python3下我们输入: ls=[1,2,3] rs=map(str,l ...

  10. python基础知识点四

    网络编程(socket) 软件开发的架构: 两个程序之间通讯的应用大致通过从用户层面可以分为两种: 1是C/S,即客户端与服务端,为应用类的,比如微信,网盘等需要安装桌面应用的 2是B/S,即浏览器与 ...