• CF796C 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 a i.

    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:

    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 a 1, a 2, ..., a n ( - 109 ≤ a i ≤ 109) — the strengths of the banks.

    Each of the next n - 1 lines contains two integers u i and v i (1 ≤ u i, v i ≤ n, u i ≠ v i) — meaning that there is a wire directly connecting banks u i and v i.

    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条边组成的树,每个点价值为a[i],如果两点有边直接相连,这两点算相邻,如果存在三个点i,j,kik相邻,且 j也与k相邻则ij算半相邻。

    刚开始,每个点都在线状态,你有一个初始值x(待求),可以选择任意一点i(a[i]<=x)进行攻击,攻击i后,和i相邻和半相邻的点a[]++,同时i处于下线状态,要求攻击n个点需要的最小x

    除了第一次外,每次攻击的点i必须满足:

    1. i必须在线
    2. i必须和某个处于离线状态的点相邻,
    3. a[i]<=x

    我们把所有电脑中最高的强度定为first,把first-1定为second

    把强度为first的电脑的个数定为fcnt,把强度为second的电脑的个数定为scnt

    我们分析一下,不难发现有一下三种情况

    1、答案为first

    一、fcnt=1并且scnt=0

    二、fcnt=1并且scnt不为0但是所有强度为second的电脑都与唯一强度为first的电脑直接相连

    2、答案为first+1

    一、fcnt=1并且scnt不为0并且所有强度为second的电脑不能与唯一强度为first的电脑直接相连

    二、fcnt>1并且所有强度为first的电脑都与某一台电脑直接相连

    3、答案为first+2

    fcnt>1并且所有强度为first的电脑不都与某一台电脑直接相连

    代码

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int maxn=6e5+5;
    ll a[maxn],head[maxn],tot=1;
    struct asd{
    	ll to,next;
    }b[maxn];
    void ad(ll aa,ll bb){
    	b[tot].to=bb;
    	b[tot].next=head[aa];
    	head[aa]=tot++;
    }
    int main(){
    	memset(head,-1,sizeof(head));
    	ll n;
    	scanf("%lld",&n);
    	ll fir=-0x3f3f3f3f3f3f3f3f,sec;
    	for(ll i=1;i<=n;i++){
    		scanf("%lld",&a[i]);
    		fir=max(fir,a[i]);
    	}
    	for(ll i=1;i<n;i++){
    		ll aa,bb;
    		scanf("%lld%lld",&aa,&bb);
    		ad(aa,bb);
    		ad(bb,aa);
    	}
    	ll fcnt=0,scnt=0;
    	for(ll i=1;i<=n;i++){
    		if(fir-a[i]==1) scnt++;
    		if(fir==a[i]) fcnt++;
    	}
    	sec=fir-1;
    	ll jl=0;
    	if(scnt){
    		for(ll i=1;i<=n;i++){
    			if(a[i]==fir){
    				jl=i;
    				break;
    			}
    		}
    		for(ll i=head[jl];i!=-1;i=b[i].next){
    			ll u=b[i].to;
    			if(a[u]==sec){
    				scnt--;
    			}
    		}
    	}
    	if(scnt==0 && fcnt==1){
    		printf("%lld
    ",fir);
    		return 0;
    	}
    	else if(scnt!=0 && fcnt==1){
    		printf("%lld
    ",fir+1);
    		return 0;
    	}
    	for(jl=1;jl<=n;jl++){
    		ll js=0;
    		if(a[jl]==fir) js++;
    		for(ll i=head[jl];i!=-1;i=b[i].next){
    			ll u=b[i].to;
    			if(a[u]==fir){
    				js++;
    			}
    		}
    		if(fcnt==js){
    			printf("%lld
    ",fir+1);	
    			return 0;
    		}
    	}
    	printf("%lld
    ",fir+2);	
    	return 0;
    }
    
  • 相关阅读:
    python计算机视觉项目实践
    Codeforces Round #256 (Div. 2) B (448B) Suffix Structures
    SonarLint插件的安装与使用
    后缀表达式求值
    有用代码段2
    提高Java代码质量的Eclipse插件之Checkstyle的使用具体解释
    Intellij Idea搭建Spark开发环境
    代码备忘, TODO宏实现
    浏览器自己主动填表安全漏洞:查看浏览器保存的password
    PDO 查询mysql返回字段整型变为String型解决方法
  • 原文地址:https://www.cnblogs.com/liuchanglc/p/12806661.html
Copyright © 2020-2023  润新知