博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF-796C
阅读量:5153 次
发布时间:2019-06-13

本文共 3991 字,大约阅读时间需要 13 分钟。

C. Bank Hacking
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.

There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.

Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.

When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.

To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met:

  1. Bank x is online. That is, bank x is not hacked yet.
  2. Bank x is neighboring to some offline bank.
  3. The strength of bank x is less than or equal to the strength of Inzane's computer.

Determine the minimum strength of the computer Inzane needs to hack all the banks.

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.

The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.

Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — meaning that there is a wire directly connecting banks ui and vi.

It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.

Output

Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.

Examples
input
5 1 2 3 4 5 1 2 2 3 3 4 4 5
output
5
input
7 38 -29 87 93 39 28 -55 1 2 2 5 3 2 2 4 1 7 7 6
output
93
input
5 1 2 7 6 7 1 5 5 3 3 4 2 4
output
8
Note

In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how:

  • Initially, strengths of the banks are [1, 2, 3, 4, 5].
  • He hacks bank 5, then strengths of the banks become [1, 2, 4, 5,  - ].
  • He hacks bank 4, then strengths of the banks become [1, 3, 5,  - ,  - ].
  • He hacks bank 3, then strengths of the banks become [2, 4,  - ,  - ,  - ].
  • He hacks bank 2, then strengths of the banks become [3,  - ,  - ,  - ,  - ].
  • He completes his goal by hacking bank 1.

In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93.

 

 

 

题意:

有n家银行,每个银行都有自己的权值,当我们攻击一个银行时,跟他距离为1和2的银行权值都会+1。

只有在我们本身权值大于银行权值时才可以攻击,求本身至少需要多少权值。

 

可以将所有的银行关系看成一棵树,则与根节点距离为一的银行最终会+1,其余的+2.

由此我们就有maxn,maxn+1,maxn+2三种可能的结果。

当最大值maxn只有一个时,若所有maxn-1距离maxn都为1,则本身需要maxn,否则需要maxn+1。

当最大值大于一个时,若存在一点,其本身和距离为1的点包含了所有maxn,则只需要maxn+1,

否则maxn+2。

 

附AC代码:

1 #include
2 using namespace std; 3 4 const int N=300010; 5 6 vector
mp[N]; 7 int a[N]; 8 9 10 int main(){11 int n,x,y,u,ans;12 int maxn=-1000000010;13 cin>>n;14 for(int i=1;i<=n;i++){15 mp[i].clear();16 cin>>a[i];17 maxn=max(a[i],maxn);18 }19 for(int i=1;i
>x>>y;21 mp[x].push_back(y);22 mp[y].push_back(x);23 }24 int ma=0,mb=0;25 for(int i=1;i<=n;i++){26 if(a[i]==maxn)27 ma++,u=i;28 if(a[i]==maxn-1)29 mb++;30 }31 if(ma==1){32 int cont=0;33 for(int i=0;i

 

转载于:https://www.cnblogs.com/Kiven5197/p/6802522.html

你可能感兴趣的文章
Mac 关机卡住
查看>>
Python--进程与线程
查看>>
ssm开发随笔
查看>>
fidder使用
查看>>
circos的ubuntu和mac安装
查看>>
C - Heavy Transportation
查看>>
ubuntu的home目录下,Desktop等目录消失不见
查看>>
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
MongoDB快速入门学习笔记2 MongoDB的概念及简单操作
查看>>
2017.3.24上午
查看>>
[资源]开源项目汇总
查看>>
Spring学习整合hibernate(四)
查看>>
K-mean聚类算法汇聚有用信息——学习笔记
查看>>
树莓派修改VNC分辨率
查看>>
构建之法阅读笔记04
查看>>
人月神话阅读笔记03
查看>>
CSS3实现图片黑白滤镜居中,hover缩放遮罩的效果
查看>>
【JMeter4.0】一、JAVA环境-JDK1.10安装与配置
查看>>
150 Opening ASCII mode data connection. FTP连接的PASV和PORT方式
查看>>