http://codeforces.com/contest/796/problem/C

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:

  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.

题意:

一只狗要入侵银行抢钱,一开始每个银行都是在线的,银行与银行直接通过电线相连,如果银行 i 和 j 直接连接,就称它们为相邻的,如果i 和 j 之间通过了 k 相连,就称它们为半相邻。

现在每个银行有个初始能力值a,狗的电脑也有能力值,入侵的条件是能力值不小于银行的能力值。一开始狗可以随意选择一个银行作为起点,然后该银行下线,与它相邻和半相邻的银行的能力值都加1。

现在要求狗的电脑的最小能力值使得可以成功入侵所有银行。

思路:

首先要能看出这道题目的模型就是树。

一开始我们随意选择选择一个银行作为起点,遍历完树上所有结点后,你会发现与它相邻的银行会变为a+1,其余都会变成a+2。

我们找到一开始所有银行中的最大值MAX,答案只可能是MAX, MAX+1, MAX+2当中的一个。

① 如果为MAX,那么MAX为根,而且所有的MAX-1都必须和根相连。

② 如果为MAX+1,存在那么一个根,所有的MAX都和它相连。

③ 其余的就是MAX+2。

那么,我们只需要枚举一遍,将每个银行作为起点判断一下其最小值即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn=*1e5+; int a[maxn];
int n,m,k;
vector<int> g[maxn]; int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
int MAX = -1e9-;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i] > MAX) MAX = a[i];
} for(int i=;i<=n;i++) g[i].clear(); for(int i=;i<n;i++)
{
int u,v;
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
} int sum1=,sum2=;
for(int i=;i<=n;i++)
{
if(a[i]==MAX) sum1++;
else if(a[i]==MAX-) sum2++;
} bool flag=true;
if(sum1==) //如果只有一个最大值,判断所有MAX-1是否和它相连
{
for(int u=;u<=n;u++)
{
int c=;
if(a[u]==MAX)
{
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(a[v]==MAX-) c++;
}
if(c==sum2)
{
printf("%d\n",MAX);
flag=false;
}
}
}
} if(flag) //如果有多个最大值,判断是否存在一个根,所有MAX都与它相连
for(int u=;u<=n;u++)
{
int c=;
for(int i=;i<g[u].size();i++)
{
int v=g[u][i];
if(a[v]==MAX) c++;
}
if(sum1==c || (sum1==c+&&a[u]==MAX))
{
printf("%d\n",MAX+);
flag=false;
break;
}
} if(flag) printf("%d\n",MAX+); //其余情况
}
return ;
}

Codeforces Round #408 (Div. 2) C. Bank Hacking的更多相关文章

  1. Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)

    题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...

  2. Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)

    传送门 题意 给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring, 每家银行hack的难度为 ...

  3. Codeforces Round #408 (Div. 2) A B C 模拟 模拟 set

    A. Buying A House time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. Codeforces Round #408 (Div. 2)

    C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...

  5. Codeforces Round #408 (Div. 2) 题解【ABCDE】

    A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...

  6. Codeforces Round #408 (Div. 2) C

    Description Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. ...

  7. Codeforces Round #408 (Div. 2)(A.水,B,模拟)

    A. Buying A House time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  8. Codeforces Round #408 (Div. 2) D - Police Stations

    地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...

  9. Codeforces Round #408 (Div. 2) B

    Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, n ...

随机推荐

  1. 【BZOJ3958】[WF2011]Mummy Madness 二分+扫描线+线段树

    [BZOJ3958][WF2011]Mummy Madness Description 在2011年ACM-ICPC World Finals上的一次游览中,你碰到了一个埃及古墓. 不幸的是,你打开了 ...

  2. JFinal的启动源码解读

    本文对Jfinal的启动源码做解释说明. PS:Jfinal启动容器可基于Tomcat/Jetty等web容器启动,本文基于Jetty的启动方式做启动源码的解读和分析,tomcat类似. 入口  JF ...

  3. wampserver3 集成环境 启动Apache失败

    前提:安装完成后,原先是能够启动服务,但是按照网上教程修改conf文件后就不能启动Apache, 方法: 1.查看Apache错误日志(无奈的是看不懂) 2.在cmd命令行中查看,(打开cmd,输入: ...

  4. 10.Curator队列

        Curator也提供ZK Recipe的分布式队列实现.利用ZK的 PERSISTENTSEQUENTIAL节点,可以保证放入到队列中的项目是按照顺序排队的.如果单一的消费者从队列中取数据,那 ...

  5. 170518、FastDFS_配置文件详解

    http://bbs.chinaunix.net/thread-1941456-1-1.html 首先是 tracker.conf # is this config file disabled # f ...

  6. SQL Server的差异备份还原

    在SQL Server中还原差异备份,需要先还原在差异备份时间点之前的一个完整备份,在还原完整备份时要加上NORECOVERY参数,示例SQL语句如下: RESTORE DATABASE [数据库名称 ...

  7. vs 开发常用快捷键

    alt+shift+enter    编辑区最大化ctrl+]        括号匹配 ctrl+j        强迫智能感知ctrl+shift+空格    强迫智能感知(参数) ctrl+k+d ...

  8. android 导出数据库文件

    1.打开dos窗口,进入自己SDK路径下,再进入platform-tools下边 2.进入shell模式: adb shell 3.获取所有root权限: su root 4.打开需要导出的数据库文件 ...

  9. Instagram的技术探索(转)

    add by zhj: 略有修改 原文:http://www.cnblogs.com/xiekeli/archive/2012/05/28/2520770.html 前一篇翻译了Instagram b ...

  10. AE开发的一个想法

    基于字典进行GIS图形进行编辑. 图层信息 大类别 字典项(属性字段) 居民点 控制点 GPS控制点 线状道路 铁路 省道 国道 一般公路 名称 长度 等级 备注 线状水系 面状道路 面状水系 湖泊 ...