Codeforces Round #408 (Div. 2) C. Bank Hacking(分类讨论)

来源:互联网 发布:ipad做笔记软件 编辑:程序博客网 时间:2024/06/06 11:41

C. Bank Hacking
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.

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个点,和每个点的权值。n-1条边。我们要占领所有的点。规则如下:
1:你的权值>=该点权值。
2:这个点周围被攻击过。
每当一个点被占领,与它距离为1的点权值+1,距离为2的点权值+1.
问你需要的最小权值。
题解:
简单分析一波可以得出,除了第一个点权值不变,与它距离为1的点+1,其余+2.
所以答案在max,max+1,max+2之间。
设maxn的个数为a,maxn-1的个数为b。
1:如果a等于1,且b个点都与它距离为1.则答案为max。
2:a==1,小于b个点距离为1,输出max+1,或者存在一个点,使得它和距离为1的点中max的个数为a。
3:其余输出max+2。
代码:

#include<bits/stdc++.h>using namespace std;const int N=350000;int n,x,y;int a[N];vector<int>mp[N];int main(){    scanf("%d",&n);    int maxn=-1000005000;    for(int i=1;i<=n;i++) mp[i].clear(),scanf("%d",&a[i]),maxn=max(maxn,a[i]);    for(int i=1;i<n;i++)    {        scanf("%d%d",&x,&y);        mp[x].push_back(y);        mp[y].push_back(x);    }    int mx,mc;    mx=mc=0;    int u,ans;    for(int i=1;i<=n;i++)    {        if(a[i]==maxn) mx++,u=i;        if(a[i]==maxn-1) mc++;    }    if(mx==1)    {        int cont=0;        for(int i=0;i<mp[u].size();i++)        {            int v=mp[u][i];            if(a[v]==maxn-1) cont++;        }        if(cont==mc) ans=maxn;        else            ans=maxn+1;        printf("%d\n",ans);    }    else    {        bool flag=false;        for(int i=1;i<=n;i++)        {            int cont=0;            if(a[i]==maxn) cont++;            for(int j=0;j<mp[i].size();j++)            {                int v=mp[i][j];                if(a[v]==maxn)cont++;            }            if(cont==mx) flag=true;        }        if(flag) printf("%d\n",maxn+1);         else  printf("%d\n",maxn+2);    }}
0 0
原创粉丝点击