codeforce 796C Bank Hacking 智力題orz

題目:

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-neighboringto 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 423157, and 6, in this order. This way, he can hack all banks using a computer with strength 93.


答案tutotial已經說得很清楚了,關鍵是意識到,如果v是被hack的點,hack完整個樹後,那麼與它(v)直接相鄰的點的strenth+1,不直接相鄰的點strenth+2.然後就是分別考慮有多個最大值和單個最大值。

case1:單個最大值。那麼就考慮,1,次小值是不是爲max-1,如果是 再考慮是不是所有次小值都連在了max上,如果是,res=max。否則,res=max+1.2,如果次小值不是max-1,res=max.

case2: 多個最大值。那麼就考慮,1,所有最大值點是否連在一個點上(可能這個點本身就是最大值點,也可能不是)如果是,res=max+1,否則res=max+2.

code:

哎,挫代碼,還是不要參考了。

#include<cstdio>
#include<map>
#include<cstring>
#define max(a,b) (a>b?a:b)
using namespace std;
typedef long long LL;
const int MAXN=3e5+5;
const LL INF=1e10+5;
long long  a[MAXN],head[MAXN],w[MAXN];
struct edge{int v,next;};
struct edge es[MAXN*2];
int tot;
map<LL ,int>cnt;
void init(){
    tot=0;
    memset(head,-1,sizeof(head));
}
void addEdge(int a,int b){
    es[tot].v=b;
    es[tot].next=head[a];
    head[a]=tot++;
}
bool flag;
LL big0,big2;
void dfs0(int x){
    int maxCnt=0;
    if(a[x]==big0)++maxCnt;
    for(int i=head[x];i!=-1;i=es[i].next){
        int to=es[i].v;
        if(a[to]==big0)++maxCnt;
    }
    if(!flag){
        flag=(maxCnt==cnt[big0]);
    }
}
void dfs2(int x){
    int big2Cnt=0;
    for(int i=head[x];i!=-1;i=es[i].next){
        int to=es[i].v;
        if(a[to]==big2)++big2Cnt;
    }
    if(!flag){
        flag=(big2Cnt==cnt[big2]);
    }
}
int main(void){
    int n;scanf("%d",&n);
    big0=-INF,big2=-INF;
    for(int i=1;i<=n;++i){
        scanf("%I64d",a+i);
        cnt[a[i]]++;
        if(a[i]>=big0){
            big2=max(big2,big0);
            big0=a[i];
        }else if(a[i]>big2)big2=a[i];
    }
    //printf("big0=%I64d,big2=%I64d\n",big0,big2);
    init();
    for(int i=1;i<n;++i){
        int a,b;scanf("%d%d",&a,&b);
        addEdge(a,b);
        addEdge(b,a);
    }
    flag=false;
    if(cnt[big0]>1){
        for(int i=1;i<=n;i++){
            dfs0(i);
        }
        printf("%I64d\n",flag?big0+1:big0+2);
    }
    else{
        if(big0-big2==1){
            for(int i=1;i<=n;++i){
                if(a[i]==big0)dfs2(i);
            }
            printf("%I64d\n",flag?big0:big0+1);
        }
        else{
            printf("%I64d\n",big0);
        }
    }
}


另外就是注意初始化的問題,例如數組大小,以及big0,big1的初始化。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章