codeforce 796C

来源:互联网 发布:刺客列传网络剧百度云 编辑:程序博客网 时间:2024/05/19 12:40

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.

这其实是一棵无根树,答案只有Max,   Max+1, Max+2;(Max表示这些权值中最大的)

然后接下来是来一遍预处理。求出各个点中与其相邻Max,Max-1的个数,包含当前节点。接下来设所有点中Max的个数为sum1, Max-1的个数为sum2;

(1)当答案为Max时,这时得有sum1==1,且当前点的子节点包含了全部的值为Max-1的节点。

(2)当答案为Max+1时,这时候要有一个节点的子节点包含了所有的值Max的节点。

(3)剩下的就是答案为Max+2;

AC代码:

#include<stdio.h>#include<vector>using namespace std;struct node{int M1, M2;};int n, a[300010], Max=-2000000000;;vector<int> g[300010];node s[300010];int sum1, sum2;int main(){int i, j, k, u, v;scanf("%d", &n);for(i=1; i<=n; i++){scanf("%d", &a[i]);if(a[i]>Max) Max=a[i];}for(i=1; i<=n-1; i++){scanf("%d%d", &u, &v);g[u].push_back(v);g[v].push_back(u);}for(i=1; i<=n; i++){if(a[i]==Max)sum1++;if(a[i]==Max-1)sum2++;}for(i=1; i<=n; i++){int Size=g[i].size();int c1=0, c2=0;if(a[i]==Max)c1++;if(a[i]==Max-1)c2++;for(j=0; j<Size; j++){if(a[g[i][j]]==Max) c1++;if(a[g[i][j]]==Max-1) c2++;}s[i].M1=c1;s[i].M2=c2;}int root;for(i=1; i<=n; i++){if(a[i]==Max){root=i;break;}}    if(sum1==1){    if(s[root].M2==sum2){    printf("%d", Max);    return 0;}}for(i=1; i<=n; i++){if(s[i].M1==sum1){printf("%d", Max+1);return 0;}}printf("%d", Max+2);return 0;}




0 0
原创粉丝点击