Codeforces Round #408 (Div. 2) C. Bank Hacking

来源:互联网 发布:windows播放器下载 编辑:程序博客网 时间:2024/06/06 12:23


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 neighboringand 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个银行,n-1条边,也就是一棵树,每个银行都有一个权值,只有当能力值大于等于这个数值,你才能抢劫这个银行。每当抢劫完这个银行,与它相邻的和半相邻的银行权值都+1,并且下一次抢劫只能抢与它相邻的银行。(半相邻的定义是相邻的点再相邻)求最少需要的能力值能把全部银行抢劫完毕。

思路:我们很容易能发现,任意选则一个点的话,那么该点所需的能力值为a[i],与它相邻所需的能力值为a[i]+1,其余都是a[i]+2,所以答案只有三种,必定是max,max+1,max+2。

max:只存在一个点的权值是max,相邻的点包含了全部max-1的点。

max+1:存在一个点,它与它相邻的点包含了全部权值为max的点。

其余情况均为max+2。下面给代码:

#include<iostream>  #include<cmath>  #include<queue>  #include<cstdio>  #include<queue>  #include<algorithm>  #include<cstring>  #include<string>  #include<utility>#include<map>#include<vector>#define maxn 300005#define inf 0x3f3f3f3f  using namespace std;typedef long long LL;const double eps = 1e-8;int a[maxn], head[maxn], len, maxnum, sum, sum1, index, cal, vis[maxn];struct node{int v, next;}p[maxn << 1];void addedge(int u, int v){p[len].v = v;p[len].next = head[u];head[u] = len++;}int main(){int n;scanf("%d", &n);memset(head, -1, sizeof(head));maxnum = -inf;for (int i = 1; i <= n; i++){scanf("%d", &a[i]);maxnum = max(maxnum, a[i]);}for (int i = 1; i <= n; i++){if (a[i] == maxnum){sum++;index = i;}else if (a[i] == maxnum - 1)sum1++;}for (int i = 0; i < n - 1; i++){int u, v;scanf("%d%d", &u, &v);addedge(u, v);addedge(v, u);}int judge = 2;if (sum == 1){for (int i = head[index]; ~i; i = p[i].next){if (a[p[i].v] == maxnum - 1)cal++;}if (cal == sum1)judge = 0;elsejudge = 1;}else{for (int i = 1; i <= n; i++){if (a[i] == maxnum){vis[i]++;if (vis[i] == sum){judge = 1;break;}for (int j = head[i]; ~j; j = p[j].next){vis[p[j].v]++;if (vis[p[j].v] == sum){judge = 1;break;}}if (judge == 1)break;}}}printf("%d\n", maxnum + judge);}



0 0
原创粉丝点击