JZOJ 【2020.11.30提高组模拟】剪辣椒(chilli)
题目大意
给出一棵 \(n\) 个节点的树,删去其中两条边
使得分出的三个子树大小中最大与最小的差最小
分析
先一边 \(dfs\) 预处理出以 \(1\) 为根每个点的 \(size\)
然后按 \(dfs\) 的顺序枚举一个点,表示删去这个点返回父亲的边
记这个点为 \(x\)
分类讨论
第一种情况是删去当前点到根的一条边,记这条边下面的点为 \(y\)
这种情况会使子树 \(y\) 包含子树 \(x\)
第二种情况是删去不包含子树 \(x\) 的已经遍历过的边
这种情况须令外处理
于是我们算答案是要做两遍 \(dfs\)
具体细节看代码
\(Code\)
#include<cstdio>
#include<iostream>
#include<set>
using namespace std;
const int N = 2e5 + 5;
int n, h[N], siz[N], ans;
set<int> s;
set<int>::iterator it;
struct edge{int to, nxt;}e[N << 1];
void add(int x, int y)
{
static int tot = 0;
e[++tot] = edge{y, h[x]}, h[x] = tot;
}
void dfs(int x, int fa)
{
siz[x] = 1;
for(register int i = h[x], v; i; i = e[i].nxt)
{
v = e[i].to;
if (v == fa) continue;
dfs(v, x), siz[x] += siz[v];
}
}
int getc(int x, int y, int z)
{
int a = max(max(x, y), z), b = min(min(x, y), z);
return a - b;
}
void calc(int x)
{
int d = (n - siz[x]) >> 1;
it = s.lower_bound(d);
if (it != s.end()) ans = min(ans, getc(siz[x], *it, n - siz[x] - *it));
if (it != s.begin()) --it, ans = min(ans, getc(siz[x], *it, n - siz[x] - *it));
}
void dfs1(int x, int fa)
{
calc(x);
s.insert(n - siz[x]);
for(register int i = h[x], v; i; i = e[i].nxt)
{
v = e[i].to;
if (v == fa) continue;
dfs1(v, x);
}
s.erase(n - siz[x]);
}
void dfs2(int x, int fa)
{
for(register int i = h[x], v; i; i = e[i].nxt)
{
v = e[i].to;
if (v == fa) continue;
calc(v), dfs2(v, x);
}
s.insert(siz[x]);
}
int main()
{
freopen("chilli.in", "r", stdin);
freopen("chilli.out", "w", stdout);
scanf("%d", &n);
for(register int i = 1, x, y; i < n; i++) scanf("%d%d", &x, &y), add(x, y), add(y, x);
ans = 0x3f3f3f3f, dfs(1, 0), dfs1(1, 0), s.clear(), dfs2(1, 0);
printf("%d", ans);
}
JZOJ 【2020.11.30提高组模拟】剪辣椒(chilli)的更多相关文章
- 【2020.11.30提高组模拟】剪辣椒(chilli)
剪辣椒(chilli) 题目描述 在花园里劳累了一上午之后,你决定用自己种的干辣椒奖励自己. 你有n个辣椒,这些辣椒用n-1条绳子连接在一起,任意两个辣椒通过用若干个绳子相连,即形成一棵树. 你决定分 ...
- 【2020.11.30提高组模拟】删边(delete)
删边(delete) 题目 题目描述 给你一棵n个结点的树,每个结点有一个权值,删除一条边的费用为该边连接的两个子树中结点权值最大值之和.现要删除树中的所有边,删除边的顺序可以任意设定,请计算出所有方 ...
- 【2020.11.28提高组模拟】T1染色(color)
[2020.11.28提高组模拟]T1染色(color) 题目 题目描述 给定 \(n\),你现在需要给整数 \(1\) 到 \(n\) 进行染色,使得对于所有的 \(1\leq i<j\leq ...
- 【2020.11.28提高组模拟】T2 序列(array)
序列(array) 题目描述 给定一个长为 \(m\) 的序列 \(a\). 有一个长为 \(m\) 的序列 \(b\),需满足 \(0\leq b_i \leq n\),\(\sum_{i=1}^ ...
- JZOJ 5185. 【NOIP2017提高组模拟6.30】tty's sequence
5185. [NOIP2017提高组模拟6.30]tty's sequence (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB ...
- JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift
5184. [NOIP2017提高组模拟6.29]Gift (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- JZOJ 5197. 【NOIP2017提高组模拟7.3】C
5197. [NOIP2017提高组模拟7.3]C Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...
- JZOJ 5196. 【NOIP2017提高组模拟7.3】B
5196. [NOIP2017提高组模拟7.3]B Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...
- JZOJ 5195. 【NOIP2017提高组模拟7.3】A
5195. [NOIP2017提高组模拟7.3]A Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...
- 【2020.12.03提高组模拟】A组反思
估计:40+10+0+0=50 实际:40+10+0+0=50 rank40 T1 赛时看到\(n,m\leq9\),我当机立断决定打表,暴力打了几个点之后发现在\(n\ne m\)且\(k\ne0\ ...
随机推荐
- cv2.imread opencv读取不到图片问题
解决办法 cv2.imread 路径中包含中文,改为英文 其他 这个问题解决了很久,都属于库的问题
- 关于CSDN发布博客接口的研究
前言 其实我之前就有一个想法,实现用 python 代码来发布博客, 因为我个人做了一个发布到 github 博客软件(其实就是实现 git 命令集成,还有markdown的渲染的软件), 如果我弄明 ...
- 第二章:seaborn调色板
1.系统默认调色板 1 import seaborn as sns 2 import matplotlib.pyplot as plt 3 4 # 设置画布的大小 5 sns.set(context= ...
- 2022年Kubernetes CKA 认证真题解析完整版
第一题 RBAC授权问题权重: 4% 设置配置环境:[student@node-1] $ kubectl config use-context k8s Context为部署管道创建一个新的Cluste ...
- 基础css样式
目录 css层叠样式表 css选择器 伪类选择器 选择器生效优先级 css字体颜色背景 设置宽高 边框 display属性 div盒子模型 float漂浮 溢出overflow 定位(position ...
- keepalived 主备使用
keepalived 主备使用 本篇主要介绍一下 keepalived 的基本的 主备使用 1.概述 什么是 keepalived呢,它是一个集群管理中 保证集群高可用的软件,防止单点故障,keepa ...
- Elasticsearch提示low disk watermark [85%] exceeded on [UTyrLH40Q9uIzHzX-yMFXg][Sonofelice][/Users/baid...
mac本地启动es之后发现运行一段时间一分钟就能打印好几条info日志: [2018-03-13T10:15:42,497][INFO ][o.e.c.r.a.DiskThresholdMonitor ...
- 新项目决定用 JDK 17了
大家好,我是风筝,公众号「古时的风筝」,专注于 Java技术 及周边生态. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 最近在调研 JDK 1 ...
- Java的深拷贝和浅拷贝的区别
一.拷贝的引入 (1).引用拷贝 创建一个指向对象的引用变量的拷贝. Teacher teacher = new Teacher("Taylor",26); Teacher oth ...
- Java线程诊断
1.诊断CPU占用过高 [root@master ~]# nohup java -jar JvmProject.jar & // 有问题的java程序 [1] 1627 [root@maste ...