Bank Hacking (思维题)

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 bankj are neighboring if and only if there exists a wire directly connecting them. Banki and bank j aresemi-neighboring if and only if there exists anonline bankk such that bank i and bankk are neighboring and bankk and bank j areneighboring.

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

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 integersui andvi (1 ≤ ui, vi ≤ n,ui ≠ vi) — meaning that there is a wire directly connecting banksui andvi.

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.

Example
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 strength93.

题意:

一个人要黑银行的电脑,n家银行网络相连,每家银行都有一个权值,当黑客的能力>权值才能黑银行电脑,每黑一家银行,与之相邻和次相邻的银行权值会加1,刚开始可以任意选择一家银行黑掉,但是之后黑掉的银行必须旁边有已经被黑掉的电脑,问黑客的能力至少多少才能黑掉所有银行

思路:
查询一遍,
情况一:如果MAX只有一个,并且所有MAX-1在它的旁边,那么输出值就是MAX;
情况二:如果MAX有多个,那么如果存在某个点,能够让它的子节点包含所有MAX,那么输出值就是MAX+1;
情况三:剩余所有情况为MAX+2;

另一种想法(感觉会简单很多)
从N点开始,意思就是它的相邻点+1,其余点都+2,查询最大值变可

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=3e5+10;

int n;
int a[maxn];
int next_is_max[maxn];
int next_is_max_1[maxn];
int sum1=0;
int sum2=0;
int main()
{
    cin>>n;
    int maxn=-1999999999;
    int nn;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(maxn < a[i]){
            maxn=a[i];
            nn=i;
        }
    }
    for(int i=1;i<n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        if( a[x] == maxn )
            next_is_max[y]++;
        if( a[x] == maxn-1 )
            next_is_max_1[y]++;

        if( a[y] == maxn )
            next_is_max[x]++;
        if( a[y] == maxn-1 )
            next_is_max_1[x]++;
    }
    int max_i;
    for(int i=1;i<=n;i++){
        if(a[i] == maxn){
            sum1++;
        }
        if(a[i] == maxn-1)
            sum2++;
    }
    int flag=0;
    for(int i=1;i<=n;i++){
        if(a[i] == maxn){
            if(next_is_max_1[i] == sum2 && sum1 == 1){
                printf("%d\n",maxn);
                flag=1;
                break;
            }
        }
    }
    if(!flag)
    for(int i=1;i<=n;i++){
        if(next_is_max[i]==sum1){
            printf("%d\n",maxn+1);
            flag=1;
            break;
        }
        if(a[i]==maxn && next_is_max[i]==sum1-1){
            printf("%d\n",maxn+1);
            flag=1;
            break;
        }
    }
    if(!flag)
        printf("%d\n",maxn+2);

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值