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代码:

 #include<bits/stdc++.h>
using namespace std; const int N=; vector<int> mp[N];
int a[N]; int main(){
int n,x,y,u,ans;
int maxn=-;
cin>>n;
for(int i=;i<=n;i++){
mp[i].clear();
cin>>a[i];
maxn=max(a[i],maxn);
}
for(int i=;i<n;i++){
cin>>x>>y;
mp[x].push_back(y);
mp[y].push_back(x);
}
int ma=,mb=;
for(int i=;i<=n;i++){
if(a[i]==maxn)
ma++,u=i;
if(a[i]==maxn-)
mb++;
}
if(ma==){
int cont=;
for(int i=;i<mp[u].size();i++){
int v=mp[u][i];
if(a[v]==maxn-)
cont++;
}
if(cont==mb)
ans=maxn;
else
ans=maxn+;
cout<<ans<<endl;
}
else{
bool flag=false;
for(int i=;i<=n;i++){
int cont=;
if(a[i]==maxn)
cont++;
for(int j=;j<mp[i].size();j++){
int v=mp[i][j];
if(a[v]==maxn)
cont++;
}
if(cont==ma)
flag=true;
}
if(flag){
cout<<maxn+<<endl;
}
else{
cout<<maxn+<<endl;
}
}
return ;
}

CF-796C的更多相关文章

  1. 【模拟】CF 796C Bank Hacking

    题目大意 洛谷链接 给定一棵带点权树,选出一个最佳的根节点,使得根节点的点权不变,它的儿子点权加1,其余点点权加2,并使最大点权最小,输出这个最小的最大点权. 其他见链接(懒). PS:原题面很不好总 ...

  2. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  3. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  4. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  5. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  6. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  7. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  8. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  9. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  10. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

随机推荐

  1. C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩 C# 文件压缩加解密

    C# 利用ICSharpCode.SharpZipLib实现在线加密压缩和解密解压缩   这里我们选用ICSharpCode.SharpZipLib这个类库来实现我们的需求. 下载地址:http:// ...

  2. bash的几个特殊参数和位置参量

    http://blog.csdn.net/jiankun_wang/article/details/4336285 一.$*和$@ 首先介绍两个极其相似.很难区分的特殊参数$*和$@,先看如下输出: ...

  3. 用python模拟TCP3次握手连接及发送数据

    源码如下: from scapy.all import * import logging logging.getLogger('scapy.runtime').setLevel(logging.ERR ...

  4. Nginx下的https配置

    https: https(Secure Hypertext Transfer Protocol) 安全超文本传输协议 它是以安全为目标的http通道,即它是http的安全版.它使用安全套接字层(SSL ...

  5. Android笔记之dp与px之间的转换以及LayoutParams

    dp与px之间的转换公式 px = dp * (dpi / 160) dp = px / (dpi / 160) 其中dpi的获取方式如下 private void getDpi() { Displa ...

  6. 使用Windows API发送HTTP请求

    先看一个简单的GET示例 #include <Windows.h> #include <winhttp.h> #include <stdio.h> int main ...

  7. SpringBoot-(9)-MyBatis 操作数据库

    这里仅仅以插入数据为例: 一, 创建基于MyBatis的项目 具体流程参考之前帖 二,创建Mapper接口 public interface AccountMapper { @Insert(" ...

  8. [数据挖掘课程笔记]SLIQ算法

    1.数据结构 主要的数据结构有:1.Attribute List  2.Class List 对于数据集,每一个属性都有一个对应的Attribute List.如上图所示,每个Attribute Li ...

  9. Dockerfile指令:

    Dockerfile指令: 第一行注释,指令是大写字母开头, FROM指令: FROM<image>,后面跟镜像名, FROM<image>:<tag>,后面跟镜像 ...

  10. MongoDB 倾向于将数据都放在一个 Collection 下吗?

    不是这样的. Collection 的单个 doc 有大小上限,现在是 16MB,这就使得你不可能把所有东西都揉到一个 collection 里.而且如果 collection 结构过于复杂,既会影响 ...