codeforce 796 c Bank Hacking (思维题,分类讨论)

来源:互联网 发布:asp 网页访问数据库 编辑:程序博客网 时间:2024/05/12 23:53


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
51 2 3 4 51 22 33 44 5
output
5
input
738 -29 87 93 39 28 -551 22 53 22 41 77 6
output
93
input
51 2 7 6 71 55 33 42 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 423157, and 6, in this order. This way, he can hack all banks using a computer with strength 93.


题目大意:

给你N个点,每个点的最初权值为a【i】.

现在我们要攻克这N个点,除了第一个点的攻击可以随意选择外,之后选择的攻击的点必须满足:

①这个点没有被攻击过。

②这个点的周围有点是被攻击过的。

③这个点的a【i】此时必须小于等于我们的计算机能力。

让你找到最小的计算机能力,使得攻克掉所有的点。

我们攻击掉一个点i之后,与其直接相连的点(距离为1)的a【i】都会变成a【i】+1.与其距离为2的a【i】也会变成a【i】+1.


思路:


1、根据上述题意要求三个条件,其实简化之后问题就变成了:被攻击的第一个点的a【i】不变,与这个点直接相连的a【i】都要变成a【i】+1.其余的点都变成了a【i】+2.


2、那么我们直接考虑几种极限情况:

设定maxn==max(a【i】)【1<=i<=n】,mx表示maxn的个数,mc表示maxn-1的个数。

①ans==maxn. 当且仅当mx==1.并且这个点的周围的maxn-1的个数==mc.

②ans==maxn+1.如果mx==1.并且这个点的周围的maxn-1的个数<mc.

如果存在一个点u.使得与u直接相连的点和u这个点中maxn的个数==mx.

③其余情况ans==maxn+2.


Ac代码:


#include<stdio.h>#include<vector>#include<string.h>using namespace std;vector<int >mp[300800];int a[3500000];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)mp[i].clear();        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        for(int i=1;i<=n-1;i++)        {            int x,y;            scanf("%d%d",&x,&y);            mp[x].push_back(y);            mp[y].push_back(x);        }        int maxn=-1000005000;        for(int i=1;i<=n;i++)maxn=max(maxn,a[i]);        int mx=0,mc=0;        int ans;        int u;        for(int i=1;i<=n;i++)        {            if(a[i]==maxn)mx++,u=i;            if(a[i]==maxn-1)mc++;        }        if(mx==1)        {            int cont=0;            for(int i=0;i<mp[u].size();i++)            {                int v=mp[u][i];                if(a[v]==maxn-1)cont++;            }            if(cont==mc)ans=maxn;            else ans=maxn+1;            printf("%d\n",ans);        }        else        {            int flag=0;            for(int i=1;i<=n;i++)            {                int cont=0;                if(a[i]==maxn)cont++;                for(int j=0;j<mp[i].size();j++)                {                    int v=mp[i][j];                    if(a[v]==maxn)cont++;                }                if(cont==mx)flag=1;            }            if(flag==1)printf("%d\n",maxn+1);            else printf("%d\n",maxn+2);        }    }}



0 0
原创粉丝点击