G - Bank Hacking CodeForces - 796C

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.

G - Bank Hacking CodeForces - 796C - yftianyizhicheng - 天翼之城的博客
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 neighboring and 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:

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.

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?≤?n, ui?≠?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.

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

真的被这道题坑死了,刚开始用bfs怎么也写不出啊,网上搜了才知道是有技巧的,看来想法题要多做,就是它只有两个相邻的才会加,如果只有一个最大值,只要找出它旁边的第二大值是否等于所有的第二最大值,是就相等,否则加一。如果有多个,就遍历一遍,找出是否有一个数,旁边所有最大数的和等于总共最大数的和,是就+1,否则+2.

#include<iostream>
#include<vector>
using namespace std;
vector<int>f[300005];
int maxn=-1e9,ans;
int mx[300005],secmax,a[300005];
int main()
{
     int n;
     cin>>n;
     for(int i=1;i<=n;i++)
     {
          cin>>a[i];
          if(a[i]>maxn)
              maxn=a[i];
    } 
    int ct=0;
    for(int i=1;i<=n;i++)
    {
         if(a[i]==maxn)
         {
             mx[++ct]=i;
          }
     else if(a[i]==maxn-1)
         secmax++;
     }
     for(int i=1;i<n;i++)
     {
          int x,y;
          cin>>x>>y;
          f[x].push_back(y);
          f[y].push_back(x);
     }
     int flag=0;
     if(ct==1)
     {
          int ctsecmax=0;
          for(int i=0;i<f[mx[1]].size();i++)
          {
               if(a[f[mx[1]][i]]==maxn-1)
               ctsecmax++;
          }
          if(ctsecmax==secmax)
              ans=maxn;
          else
              ans=maxn+1;
     }
     else 
     {
          for(int i=1;i<=n;i++)
          {
               int ctmax=0;
               for(int j=0;j<f[i].size();j++)
               {
                    int k=f[i][j];
                    if(a[k]==maxn)
                        ctmax++;
               }
               if(a[i]==maxn)
                   ctmax++;
               if(ctmax==ct)
               {
                    flag=1;
                    break;
               }
          }
      if(flag)
          ans=maxn+1;
      else
          ans=maxn+2;
     }
     cout<<ans<<endl;
     return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/81974399
今日推荐