Codeforces Round #408 (Div. 2) C. Bank Hacking(暴力啊!暴力)

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

把题读懂之后就想到了怎么做了,但是害怕会超时,就想优化一点的算法。其实啊直接暴力就能过了,而且不会超时的,伤心啊!!

题意:给你一系列的数表示银行的库存,然后给你一对数<u,v>表示第u个银行和第v个银行有电线相连。当且仅当存在直接连接它们的电线时,银行i和银行j是相邻的。 当且仅当存在银行i和银行k相邻并且银行k和银行j是相邻的在线银行k时,银行i和银行j是半相邻的。然后从其中一个银行开始遍历,和当前银行相邻和半相邻的银行库存增加1.问每次遍历的最大值的最小值是多少?

思路:手算几个样例就会知道:与开始银行直接相连的银行应该+1,其余银行(除了开始的银行)都应该+2。如果库存最多的银行只有一家且比其他银行的库存都多2,则结果是最多银行的库存,如果比其他银行不多2,则看一下次多银行是否和最多银行直接相连,如果直接相连则结果还是最多银行的库存,否则是最多银行库存+1,如果最多银行有多家,就遍历所有的银行,如果遍历的最大值是最大库存+1,这就直接输出,如果没找到就输出最大库存+2.说白了么就直接把所有的节点遍历一遍找最先值就好了。

 1 #include <iostream>
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 const int maxn=3e5+5;
 5 const int inf=1e9+7;
 6 int cnt[maxn],flag[maxn],a[maxn];
 7 vector<int>vec[maxn];
 8 int main()
 9 {
10     int n,maxx,u,v;
11     while(~scanf("%d",&n))
12     {
13         int maxx=-inf;
14         for(int i=1; i<=n; i++)
15         {
16             vec[i].clear();
17             scanf("%d",&a[i]);
18             maxx=max(maxx,a[i]);
19         }
20         for(int i=1; i<n; i++)
21         {
22             scanf("%d%d",&u,&v);
23             vec[u].push_back(v);
24             vec[v].push_back(u);
25         }
26         int ma=0,ma_x=0;
27         for(int i=1; i<=n; i++)
28         {
29             if(a[i]==maxx)
30                 ma++;
31             else if(a[i]+1==maxx)
32                 ma_x++;
33         }
34         if(ma==1)
35         {
36             if(ma_x==0)
37             {
38                 printf("%d\n",maxx);
39             }
40             else
41             {
42                 int sum=0;
43                 for(int i=1; i<=n; i++)
44                 {
45                     if(a[i]==maxx)
46                     {
47                         for(int k=0; k<vec[i].size(); k++)
48                         {
49                             if(a[vec[i][k]]+1==maxx)
50                                 sum++;
51                         }
52                         break;
53                     }
54                 }
55                 if(sum==ma_x)
56                 {
57                     printf("%d\n",maxx);
58                 }
59                 else
60                 {
61                     printf("%d\n",maxx+1);
62                 }
63             }
64         }
65         else
66         {
67             int sum=0,ans=maxx+2;
68             for(int i=1; i<=n; i++)
69             {
70                 sum=0;
71                 for(int k=0; k<vec[i].size(); k++)
72                 {
73                     if(a[vec[i][k]]==maxx)
74                         sum++;
75                 }
76                 if((ma-1==sum&&a[i]==maxx)||(ma==sum&&a[i]!=maxx))
77                 {
78                     ans=maxx+1;
79                 }
80                 if(ans==maxx+1)
81                     break;
82             }
83             printf("%d\n",ans);
84         }
85     }
86     return 0;
87 }
View Code

 

posted @ 2017-05-03 14:47  Wally的博客  阅读(290)  评论(0编辑  收藏  举报