首页 > 其他分享 >Game on Tree 3

Game on Tree 3

时间:2022-09-30 18:44:05浏览次数:34  
标签:Aoki Tree Vertex game Game integer piece Takahashi

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.

  1. First, Aoki chooses a non-root vertex and replaces the integer written on that vertex with $0$.
  2. Next, Takahashi moves the piece to a (direct) child of the vertex the piece is on.
  3. 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.

  1. The piece starts on Vertex $1$.
  2. Aoki changes the integer written on Vertex $7$ from $10$ to $0$.
  3. Takahashi moves the piece from Vertex $1$ to Vertex $2$.
  4. Aoki changes the integer written on Vertex $4$ from $6$ to $0$.
  5. Takahashi moves the piece from Vertex $2$ to Vertex $5$.
  6. 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);
}

标签:Aoki,Tree,Vertex,game,Game,integer,piece,Takahashi
From: https://www.cnblogs.com/mekoszc/p/16745841.html

相关文章

  • TreeView的RenderControl的问题
    TreeView,这东西,正常情况下一般是不用的,不过我们的美工,没弄个树型的样式出来,没折,将就用一下TreeView了说重点:环境搭建:一页面,拖一下TreeView控件上去,随便添加几个项。然后Page......
  • leetcode 226. Invert Binary Tree 翻转二叉树(简单)
    一、题目大意给你一棵二叉树的根节点root,翻转这棵二叉树,并返回其根节点。示例1:输入:root=[4,2,7,1,3,6,9]输出:[4,7,2,9,6,3,1]示例2:输入:root=[2,1,3]输......
  • DSU on Tree
    反正就是利用重链剖分:一个点到根最多只会经过\(\logN\)条轻边。然后就对于一个点,求其子树内的一些东西,要记录这个子树内所有节点的某些信息——这个点的重儿子递归下去......
  • leetcode -- tree 2
    最大二叉树递归构造classSolution:defconstructMaximumBinaryTree(self,nums:List[int])->Optional[TreeNode]:ifnotnums:retur......
  • 题解【CF1632E1 Distance Tree (easy version)】
    CF1632E1DistanceTree(easyversion&hardversion)解题报告。不一定更好的阅读体验。E2没有地方交了所以就交到E1了。震撼挺大的一道题,钦定\(1\)为根。先......
  • Gitee + Sourcetree 设置公钥SSH
    设置前提安装Git Git下载安装sourceTree sourceTree下载gitee账号 gitee官网Git设置公钥1.在安装好sourcetree后点击操作选择在终端中打开  2.输入配置......
  • 哥大周赛题目 0-1 Tree (BFS + 并查集)
    上周本地比赛,老wf选手都退役了,只剩我们这些22届本科升研究生来参赛了题目不是很难,11题,比之前的训练赛要简单很多,开场A了4题签到+1裸dp+1做过+1xjb乱搞。结果最后一......
  • npm ERR! ERESOLVE unable to resolve dependency tree
      处理办法npmi--legacy-peer-depsnpmieslint-plugin-vue xnull一键下载......
  • 【CF468D】 Tree
    CF468DTree以树的重心为根i和pi不能在同一个子树中贪心求出方案点击查看代码</details>#include<set>#include<stdio.h>#include<string.h>#include<alg......
  • CF468D Tree
    CF468DTree以树的重心为根i和pi不能在同一个子树中贪心求出方案点击查看代码</details>#include<set>#include<stdio.h>#include<string.h>#include<alg......