[ABC246G] Game on Tree 3
Problem Statement
There is a rooted tree with $N$ vertices, Vertex $1$ being the root.
For each $i = 1, 2, \ldots, N-1$, the $i$-th edge connects Vertex $u_i$ and Vertex $v_i$.
Each vertex other than the root has a positive integer written on it: for each $i = 2, 3, \ldots, N$, the integer written on Vertex $i$ is $A_i$.
Takahashi and Aoki will use this rooted tree and a piece to play the following game against each other.
The piece starts on Vertex $1$. Until the game ends, they repeat the following procedure.
- First, Aoki chooses a non-root vertex and replaces the integer written on that vertex with $0$.
- Next, Takahashi moves the piece to a (direct) child of the vertex the piece is on.
- Then, the game ends if the piece is on a leaf. Even if that is not the case, Takahashi can choose to end the game immediately.
At the end of the game, Takahashi's score will be the integer written at that time on the vertex the piece is on.
Takahashi wants to make his score as large as possible, while Aoki wants to make it as small as possible.
Print the score Takahashi will get when both players play optimally for their respective purposes.
Constraints
- $2 \leq N \leq 2 \times 10^5$
- $1 \leq A_i \leq 10^9$
- $1 \leq u_i, v_i \leq N$
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
$N$
$A_2$ $\ldots$ $A_N$
$u_1$ $v_1$
$u_2$ $v_2$
$\vdots$
$u_{N-1}$ $v_{N-1}$
Output
Print the answer.
Sample Input 1
7
2 4 6 5 6 10
1 2
1 3
2 4
2 5
5 6
5 7
Sample Output 1
5
Here is a possible progression of the game when both players play optimally.
- The piece starts on Vertex $1$.
- Aoki changes the integer written on Vertex $7$ from $10$ to $0$.
- Takahashi moves the piece from Vertex $1$ to Vertex $2$.
- Aoki changes the integer written on Vertex $4$ from $6$ to $0$.
- Takahashi moves the piece from Vertex $2$ to Vertex $5$.
- Takahashi chooses to end the game.
At the end of the game, the piece is on Vertex $5$, on which the integer $5$ is written at that time, so Takahashi's score will be $5$.
Sample Input 2
30
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62 32 38 84 49 93 53 26 13 25
13 15
14 22
17 24
12 3
4 3
5 8
26 15
3 2
2 9
4 25
4 13
2 10
28 15
6 4
2 5
19 9
2 7
2 14
23 30
17 2
7 16
21 13
13 23
13 20
1 2
6 18
27 6
21 29
11 8
Sample Output 2
70
首先发现如果 Aoki 漫无目的地操作,是难以移到好的地方的。所以我们可以通过二分来给他定一个目标。
设现在二分出来 Aoki 的目标是 \(x\) 分,那么所有大于等于 \(x\) 的节点都要在 Takahashi 到达这个点之前被 Aoki 删掉。那么我们记录 \(dp_i\) 为如果要使以 \(i\) 为根的子树满足要求,需要 \(i\) 上面的点多帮忙删几个点。那么\(dp_i=\max((\sum\limits_{j\in son}dp_j) -1,0)+(a[i]>t)\)。最终判断 \(dp_i\) 是否为 \(0\) 即可。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,a[N],u,v,hd[N],e_num,l,r;
struct edge{
int v,nxt;
}e[N<<1];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
}
int dfs(int x,int y,int t)
{
int ret=0;
for(int i=hd[x];i;i=e[i].nxt)
if(e[i].v!=y)
ret+=dfs(e[i].v,x,t);
return max(ret-1,0)+(a[x]>t);
}
int check(int x)
{
return dfs(1,0,x)==0;
}
int main()
{
scanf("%d",&n);
for(int i=2;i<=n;i++)
scanf("%d",a+i);
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
l=0,r=1e9+1;
while(l<=r)
{
int md=l+r>>1;
if(check(md))
r=md-1;
else
l=md+1;
}
printf("%d",l);
}
[ABC246G] Game on Tree 3的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Tree树节点选中及取消和指定节点的隐藏
指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...
随机推荐
- 我是如何使用Spring Retry减少1000 行代码
本文翻译自国外论坛 medium,原文地址:https://levelup.gitconnected.com/how-i-deleted-more-than-1000-lines-of-code-us ...
- 《Kali渗透基础》13. 无线渗透(三)
@ 目录 1:无线通信过程 1.1:Open 认证 1.2:PSK 认证 1.3:关联请求 2:加密 2.1:Open 无加密网络 2.2:WEP 加密系统 2.3:WPA 安全系统 2.3.1:WP ...
- WPF学习 - 闭坑(持续更新)
坑1:自定义控件设计原则: 既然称之为控件,那么就必定有界面与行为两部分. 界面就是展示给用户看的,用于承载类的属性.方法.事件等. 行为就是类的方法,以及这些方法需要用到的属性.字段等. WPF设计 ...
- API接口开发管理平台--多领域企业数字化管理解决方案
随着数字化时代的到来,企业需要进行数字化转型才能更好地适应市场需求和用户需求.而API接口则是数字化转型中的重要组成部分,可以帮助企业更好地管理信息,提高效率.本文将介绍挖数据解决方案--API接口开 ...
- KRPANO资源分析工具下载VR-FACTORY全景图
示:目前分析工具中的全景图下载功能将被极速全景图下载大师替代,相比分析工具,极速全景图下载大师支持更多的网站(包括各类KRPano全景网站,和百度街景) 详细可以查看如下的链接: 极速全景图下载大师官 ...
- Solution -「洛谷 P3773」「CTSC 2017」吉夫特
Description Link. 求满足 \[\prod _{i=2}^{k} \binom{a_{b_{i-1}}}{a_{b_i}} \mod 2 = \binom{a_{b_1}}{a_{b_ ...
- 【Redis】SpringBoot集成Redis事务-亲测
大家好,我是mep.今天一起来探讨一下Redis缓存的问题,SpringBoot如何集成Redis网上文章很多,基本都是介绍如何配置redisTemplate,如何调用,本文就不过多介绍了.这次我们研 ...
- 多租户基于Springboot+MybatisPlus实现使用一个数据库一个表 使用字段进行数据隔离
多租户实现方式 多租户在数据存储上主要存在三种方案,分别是: 1. 独立数据库 即一个租户一个数据库,这种方案的用户数据隔离级别最高,安全性最好,但成本较高. 优点:为不同的租户提供独立的数据库,有助 ...
- 洛谷题解 | P1051 谁拿了最多奖学金
目录 题目描述 输入格式 输出格式 输入输出样例 提示 题目思路 AC代码 题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1. 院士奖学金,每人 ...
- CCF CSP认证注册、报名、查询成绩、做模拟题等答疑
CCF CSP认证注册.报名.查询成绩.做模拟题等答疑 CCF CSP认证中心将考生在注册,或报名,或查询成绩,或历次真题练习时遇到的问题进行汇总,并给出解决方法,具体如下: 1.注册时,姓名可否随意 ...