「SCOI2005」王室联邦
「SCOI2005」王室联邦
传送门
树分块。
考虑把树,按照节点个数每 \(B\) 个分块,把块顶作为省会。
这是具体证明
参考代码:
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 1010;
int tot, head[_], nxt[_ << 1], ver[_ << 1];
inline void Add_edge(int u, int v)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
int n, B, cnt, num, rt[_], bel[_], x[_];
inline void dfs(int u, int f) {
int tmp = num;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i]; if (v == f) continue;
dfs(v, u);
if (num - tmp >= B) {
rt[++cnt] = u;
while (num > tmp) bel[x[num--]] = cnt;
}
}
x[++num] = u;
}
int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
read(n), read(B);
for (rg int u, v, i = 1; i < n; ++i)
read(u), read(v), Add_edge(u, v), Add_edge(v, u);
dfs(1, 0);
if (!cnt) rt[++cnt] = 1;
while (num) bel[x[num--]] = cnt;
printf("%d\n", cnt);
for (rg int i = 1; i <= n; ++i) printf("%d%c", bel[i], " \n"[i == n]);
for (rg int i = 1; i <= cnt; ++i) printf("%d%c", rt[i], " \n"[i == n]);
return 0;
}
「SCOI2005」王室联邦的更多相关文章
- 【BZOJ】【1086】 【SCOI2005】王室联邦
树分块 orz vfk && PoPoQQQ http://vfleaking.blog.163.com/blog/static/174807634201231684436977/ h ...
- bzoj1086【SCOI2005】王室联邦
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1086 sol :这题水水啊,直接大力DFS就行了 首先当且仅当x<B时无解 对于以x为 ...
- 「SCOI2005」栅栏
传送门 Luogu 解题思路 我们有很显然的这样一条贪心思路: 首先满足长度短的木板,因为如果可以满足长的也肯定可以满足短的,而且可能满足更多. 那么我们就会有这样的思路:枚举一条木板由哪条木板切割而 ...
- 「SCOI2005」互不侵犯 (状压DP)
题目链接 在\(N\times N\) 的棋盘里面放 \(K\)个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共\(8\) 个格子 ...
- BZOJ 1086: [SCOI2005]王室联邦
1086: [SCOI2005]王室联邦 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1399 Solved: ...
- 【块状树】BZOJ 1086: [SCOI2005]王室联邦
1086: [SCOI2005]王室联邦 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 826 Solved: ...
- 1086: [SCOI2005]王室联邦
1086: [SCOI2005]王室联邦 Time Limit: 10 Sec Memory Limit: 162 MBSec Special JudgeSubmit: 1554 Solved: ...
- P2325 [SCOI2005]王室联邦 解题报告
P2325 [SCOI2005]王室联邦 题目描述 "余"人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理. 他的国家有\(n\) ...
- bzoj1086 [SCOI2005]王室联邦 树分块
[bzoj1086][SCOI2005]王室联邦 2014年11月14日2,6590 Description “余”人国的国王想重新编制他的国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的 ...
随机推荐
- iframe内外的操作
因为iframe涉及到跨域问题,有时候有的比较多,这不今天遇到了一个问题,处在iframe里头的js要操作iframe元素,查找百度,是可以实现的: 用jQuery在IFRAME里取得父窗口的某个元素 ...
- python 的集合
set 的特点: 把不同的元素组合在一起,元素值必须是可哈希不可变的 set 的创建 s = set ('alex li') print(s) 表现形式:去重 {'e', 'i', ' ', 'l' ...
- codeforces Beautiful Numbers
来源:http://codeforces.com/problemset/problem/1265/B B. Beautiful Numbers You are given a permutat ...
- 文件服务器之fastDFS
FastDFS是一个开源的轻量级分布式文件系统,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合中小文件(建议范围:4KB < file ...
- ASP.NET Core搭建多层网站架构【7-使用NLog日志记录器】
2020/01/29, ASP.NET Core 3.1, VS2019, NLog.Web.AspNetCore 4.9.0 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站 ...
- 吴裕雄--天生自然TensorFlow2教程:函数优化实战
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...
- kaggle赛题Digit Recognizer:利用TensorFlow搭建神经网络(附上K邻近算法模型预测)
一.前言 kaggle上有传统的手写数字识别mnist的赛题,通过分类算法,将图片数据进行识别.mnist数据集里面,包含了42000张手写数字0到9的图片,每张图片为28*28=784的像素,所以整 ...
- RTT学习之SPI设备
SPI分为主.从.设备:具体又分标准SPI/DUAL SPI/QUAD SPI(用80字节的RAMrt_err_t rt_spi_take_bus(struct rt_spi_device *devi ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈) && 单调栈
嗯... 题目链接:http://poj.org/problem?id=2559 一.单调栈: 1.性质: 单调栈是一种特殊的栈,特殊之处在于栈内的元素都保持一个单调性,可能为单调递增,也可能为单调递 ...
- Chrome的一个问题
报错:Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extensi ...