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. Zoj2421 广搜

    <span style="color:#330099;">/* M - 广搜 加强 Time Limit:2000MS Memory Limit:65536KB 64b ...

  2. 笔记04 WPF的Binding

    oneWay:使用 OneWay 绑定时,每当源发生变化,数据就会从源流向目标. OneTime: 绑定也会将数据从源发送到目标:但是,仅当启动了应用程序或 DataContext 发生更改时才会如此 ...

  3. 韦东山 第9课第1节.u-boot分析之编译体验 http://www.100ask.net/index.html

    http://www.100ask.net/index.html 韦东山官网网址 http://wenku.baidu.com/view/ae78a00390c69ec3d5bb75ce.html h ...

  4. ETL Automation完整安装方法_(元数据存放在mysql数据库)

    安装前介质准备: DBI-1.636.tar.gz DBD-mysql-4.037.tar.gz ETL.tar mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz P ...

  5. wpf 模板选择器DataTemplateSelector及动态绑定使用教程

    其实也说不上算是教程了,只是把自己学习的代码拿出来分享一下,同时方便以后遇到类似问题的时候翻一下.MSDN里如是说:通常,如果有多个 DataTemplate 可用于同一类型的对象,并且您希望根据每个 ...

  6. 用Cocoapods集成XMPPFramework 遇 Module 'KissXML' not found 问题

    用Coacopods集成XMPPFramework完成后Command + B,报Module 'KissXML' not found 一般来说,通过Coacopods集成集成第三方框架,不会再有依赖 ...

  7. checkAll全选的一个小例子

    function checkAll(tag,flag) { //得到所有check var checkboxs = $(tag).closest("table").find(&qu ...

  8. 开源安卓Android流媒体音视频播放器实现声音自动停止、恢复、一键静音功能源码

    本文转自EasyDarwin团队John的博客:http://blog.csdn.net/jyt0551/article/details/60802145 我们在开发安卓Android流媒体音视频播放 ...

  9. tomcat servlet JSP common gateway interface 公共网关接口

    Tomcat主要充当servlet/JSP容器,不过它却有大量的功能可以与传统的Web服务器相媲美,对公共网关接口(Common Gateway Interface)的支持就是其中之一. 传统的Web ...

  10. mount available

    Mount (computing), the process of making a file system accessible mount (Unix), the utility in Unix- ...