I am a slow walker,but I never walk backwards. Abraham Lincoln

GeekZRF

Codeforces 796C. Bank Hacking

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.

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.

题目链接:http://codeforces.com/contest/796/problem/C

题意:有n-1条边连接编号1-n的n个点。选择一个点x作为起点进行删除,那么与x相连的点和半相连的点的权值加1。(i与k相连,k与j相连,并且k没有被删除,那么i与j半相连)。接下来要删除的点必须没有被删除并且与已经删除的点相连。黑客的权值必须大于删除点的权值,求最小的黑客的权值。

思路:因为删除的点必须满足一些条件,所以删除点的顺序为将一个点x最为树的根,那么就依次按照树的层次删除下去。所以与x相邻的点的权值均会加1,其余的点就会加2。所以只需要依次把点最为根求出最小的黑客权值。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=1e6+100,inf=0x3f3f3f3f,mod=1e9+7;
const ll MOD=1e13+10;
int a[maxn];
int sign[maxn];
map<int,int>cou;
map<int,vector<int> >G;
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n;
    scanf("%d",&n);
    int len=0;
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        if(!cou[a[i]]) sign[len++]=a[i];
        cou[a[i]]++;
    }
    sort(sign,sign+len,cmp);
    int u,v;
    for(int i=1; i<n; i++)
    {
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int mmin=inf;
    for(int i=1; i<=n; i++)
    {
        cou[a[i]]--;
        int ans=a[i];
        for(int j=0; j<G[i].size(); j++)
        {
            int to=G[i][j];
            cou[a[to]]--;
            ans=max(ans,a[to]+1);
        }
        for(int j=0; j<len; j++)
        {
            if(cou[sign[j]])
            {
                ans=max(ans,sign[j]+2);
                break;
            }
        }
        mmin=min(mmin,ans);
        cou[a[i]]++;
        for(int j=0; j<G[i].size(); j++) cou[a[G[i][j]]]++;
    }
    cout<<mmin<<endl;
    return 0;
}
View Code

 

 

 

posted on 2017-04-11 18:04  GeekZRF  阅读(474)  评论(0编辑  收藏  举报

导航