CF-796C

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.c++

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.app

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.less

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.ide

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:this

  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.spa

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.code

The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.blog

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.ci

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.input

Output

Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.

Examples
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 strength 93.

 

 

 

題意:
有n家銀行,每一個銀行都有本身的權值,當咱們攻擊一個銀行時,跟他距離爲1和2的銀行權值都會+1。

只有在咱們自己權值大於銀行權值時才能夠攻擊,求自己至少須要多少權值。

 

能夠將全部的銀行關係當作一棵樹,則與根節點距離爲一的銀行最終會+1,其他的+2.

由此咱們就有maxn,maxn+1,maxn+2三種可能的結果。

當最大值maxn只有一個時,若全部maxn-1距離maxn都爲1,則自己須要maxn,不然須要maxn+1。

當最大值大於一個時,若存在一點,其自己和距離爲1的點包含了全部maxn,則只須要maxn+1,

不然maxn+2。

 

附AC代碼:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N=300010;
 5 
 6 vector<int> mp[N];
 7 int a[N];
 8 
 9 
10 int main(){
11      int n,x,y,u,ans;
12      int maxn=-1000000010;
13      cin>>n;
14      for(int i=1;i<=n;i++){
15          mp[i].clear();
16          cin>>a[i];
17          maxn=max(a[i],maxn);
18     }
19     for(int i=1;i<n;i++){
20         cin>>x>>y;
21         mp[x].push_back(y);
22         mp[y].push_back(x);
23     }
24     int ma=0,mb=0;
25     for(int i=1;i<=n;i++){
26         if(a[i]==maxn)
27         ma++,u=i;
28         if(a[i]==maxn-1)
29         mb++;
30     }
31     if(ma==1){
32         int cont=0;
33         for(int i=0;i<mp[u].size();i++){
34             int v=mp[u][i];
35             if(a[v]==maxn-1)
36             cont++;
37         }
38         if(cont==mb)
39         ans=maxn;
40         else
41         ans=maxn+1;
42         cout<<ans<<endl;
43     }
44     else{
45         bool flag=false;
46         for(int i=1;i<=n;i++){
47             int cont=0;
48             if(a[i]==maxn)
49             cont++;
50             for(int j=0;j<mp[i].size();j++){
51                 int v=mp[i][j];
52                 if(a[v]==maxn)
53                 cont++;
54             }
55             if(cont==ma)
56             flag=true;
57         }
58         if(flag){
59             cout<<maxn+1<<endl;
60         }
61         else{
62             cout<<maxn+2<<endl;
63         }
64     }
65     return 0;
66 }
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息