[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 ...
随机推荐
- jQuery实现购物车物品数量的加减
基于jquery的一款代码,实现购物车数据的加减,在淘宝网.京东商城购物时时经常见到的一个功能,点击文本框两侧的“+”与“-”,就可以增加或减少文本框内的数字值,每次步长为1,当然这个是可以自己设置的 ...
- Python 两个星号(**)的 参数
将参数以字典的形式导入
- 使用Python在自己博客上进行自动翻页
先上一张代码及代码运行后的输出结果的图! 下面上代码: # coding=utf-8 import os import time from selenium import webdriver #打开火 ...
- spring 的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java ...
- HTML响应式布局实现详解
摘自:https://blog.csdn.net/lesouls/article/details/81454568 第一步:在网页代码的头部,加入一行viewport元标签(1)viewport是网页 ...
- vue-i18n安装配置,运行
需求:根据浏览器语言自动切换语言 1.安装vue-i18n, yarn安装 $ yarn add vue-i18n npm安装 $ npm install vue-i18n 2.导入语言包 src下创 ...
- Html p 标签
Html p 标签 <html> <body> <!-- p标签:输出跳到下一段落--> <p>内容</p> 注:段落之前各空出一行. &l ...
- 剑指offer(10)矩形覆盖
题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 题目分析 当然也可以逆向思维 应为可以横着放或竖着放,多以f ...
- wepy开发小程序eslint报错error 'getApp' is not defined no-undef
wepy开发小程序使用getApp().globalData保存全局数据很方便,但是会在控制台看到很多报错:“error 'getApp' is not defined no-undef”,这是esl ...
- 使用vue.js + laravel开发单页面应用
最近学了vuejs和laravel,然后顺便就撸了简单的demo,这里将会根据这个demo介绍下如何使用vuejs+laravel开发一个简单的单页面应用,demo的github地址是https:// ...