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:
- Bank x is online. That is, bank x is not hacked yet.
- Bank x is neighboring to some offline bank.
- 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.
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.
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
5 1 2 3 4 5 1 2 2 3 3 4 4 5
5
7 38 -29 87 93 39 28 -55 1 2 2 5 3 2 2 4 1 7 7 6
93
5 1 2 7 6 7 1 5 5 3 3 4 2 4
8
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.
题意:
思路:
查询一遍,
情况一:如果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;
}