E - Bank Hacking CodeForces - 796C

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.

思路:一开始想到hack一家店只会影响到与之相连和间接相连的两层,由于它是一颗树。那么每一个点只会有+0,+1,+2三种可能的情况。那么最终的答案也只有三种情况。设点的最大值为maxn。则只会有maxn,maxn+1,maxn+2 三种答案 下面分类讨论:

1.maxn: 当只存在一个最大值时 ,当值为maxn-1的点的个数全与maxn直接相连时成立。

2.maxn+1:  1>当只存在一个最大值时 ,当值为maxn-1的点的个数不完全与maxn直接相连 (下面的代码并没有体现这一条,因为当最大值只有一个时,排除了上面maxn的情况,那么剩下的就是maxn+1)
                   2>  当多个最大值时,存在一个最大值与其余所有最大值的点直接相,或者存在一个节点与所有最大值点直接相连

3.maxn+2    为其余所有情况

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll fir[1008611],sec[1008611];
ll a[1008611],maxn=-1*inf;
int main(){
    ll n;
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>a[i];
        if(maxn<a[i])
            maxn=a[i];
    }
    ll fir_sum=0,sec_sum=0;
    for(int i=1;i<=n;i++){
        if(a[i]==maxn){
            fir_sum++;//为最大值的点的总个数
        }
        if(a[i]==maxn-1){
            sec_sum++;//为最大值-1的点的总个数
        }
    }
    ll u,v;
    for(ll i=1;i<n;i++){
        cin>>u>>v;
        if(a[u]==maxn){
            fir[v]++;//表示与v相连的点为 最大值的点 的个数
        }
        if(a[v]==maxn){
            fir[u]++;
        }
        if(a[u]==maxn-1){
            sec[v]++;//表示与v相连的点为 最大值-1的点 的个数
        }
        if(a[v]==maxn-1){
            sec[u]++;
        }
    }
    for(int i=1;i<=n;i++){
        if(fir_sum==1&&sec[i]==sec_sum&&a[i]==maxn){
            cout<<maxn<<endl;
            return 0;
        }
    }
    for(int i=1;i<=n;i++){

        if(fir_sum==fir[i]){
            cout<<maxn+1<<endl;
            return 0;
        }
        if(fir_sum-1==fir[i]&&a[i]==maxn){
            cout<<maxn+1<<endl;
            return 0;

        }
    }
    cout<<maxn+2<<endl;
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值