[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 ...
随机推荐
- RabbitMQ:Docker环境下搭建rabbitmq集群
RabbitMQ作为专业级消息队列:如何在微服务框架下搭建 使用组件 文档: https://github.com/bijukunjummen/docker-rabbitmq-cluster 下载镜像 ...
- Windows平台搭建Kafka
1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.htm ...
- rocketmq源码打包步骤
1,从git上面克隆好源码之后,进入rocketmq目录,执行: mvn -Prelease-all -DskipTests clean install 2,打包完成之后,进入distribution ...
- Windbg程序调试系列1-常用命令说明&示例
Windbg程序调试是.Net高级开发需要掌握的必备技能,分析内存泄露.分析高CPU.分析线程阻塞.分析内存对象.分析线程堆栈.Live Dedugging.这个领域可以说一个技能+场景化应用的结合, ...
- py-faster-rcnn
踩坑: 1. 服务器上训练: sh ./experiments/scripts/faster_rcnn_end2end.sh 会各种报错 有说是因为#!/bin/bash的问题,改过,不行. 改成如下 ...
- flask 在视图函数里操作数据库
在视图函数里操作数据库 在视图函数里操作数据的方式和在python shell中的联系基本相同,只不过需要一些额外的工作.比如把查询结果作为参数 传入模板渲染出来,或是获取表单的字段值作为提交到数据库 ...
- 《CSS世界》读书笔记(六)
<!-- <CSS世界> 张鑫旭著 --> min-width/max-width和min-height/max-height min-width/max-width出现的场景 ...
- ACM总结——2017区域赛网络赛总结
从省赛回来至今4周,每周周末都在打网络赛,每次都是划水,总结下自己弱弱的ACM吧!划水水~~ 首先是新疆赛区,基本上都是图论相关的东西,全靠队友,自己翻水水,实力躺了5道. 然后是沈阳赛区,终于有点贡 ...
- Vue:(四)Ajax(Vue-Resource)
Vue 要实现异步加载需要使用到 vue-resource 库.(挂载到vue实例上) (一)Vue-Resource引入 <script src="https://cdn.stati ...
- robot framework学习二-----元素定位
文章摘自:https://www.cnblogs.com/fnng/p/3901391.html 不要误认为Robot framework 只是个web UI测试工具,更正确的理解Robot fram ...