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. 解决jquery在IE下removeAttr不生效的问题

    使用jquery动态操纵DOM的时候在IE下会遇到remvoeAttr() 不生效的问题, 解决的办法是使用prop()方法: var node = $("div>input" ...

  2. 170714、springboot编程之多数据源切换(动态)

    (1)新建maven java project; 新建一个maven project,取名为:spring-boot-multi-ds (2)在pom.xml添加依赖包: 在pom.xml文件中加入依 ...

  3. centos7 iptables/firewalld docker open port

    here are multiple "hackish" ways to do it: scan kernel logs, as mentioned by Jiri (but you ...

  4. CRM - 销售与客户

    一.销售与客户 - 表结构 ---公共客户(公共资源) 1.没有报名 2.3天没有跟进 3.15天没有成单 客户分布表 龙泰 男 yuan 2018-5-1 3天未跟进 龙泰 男 三江 2018-5- ...

  5. MegaCli 监控raid状态 限戴尔服务器

    MegaCli 监控raid状态 MegaCli是一款管理维护硬件RAID软件,可以通过它来了解当前raid卡的所有信息,包括 raid卡的型号,raid的阵列类型,raid 上各磁盘状态,等等.通常 ...

  6. 一个用于提取简体中文字符串中省,市和区并能够进行映射,检验和简单绘图的python模块

    简介 一个用于提取简体中文字符串中省,市和区并能够进行映射,检验和简单绘图的python模块. 举个例子: ["徐汇区虹漕路461号58号楼5楼", "泉州市洛江区万安塘 ...

  7. Struts2的ActionContext

    web资源:HttpServletRequest,HttpSession,ServletContext等原生Servlet API Action中如何访问web资源: 1.和Servlet API解耦 ...

  8. Mirror--日志流压缩

    在SQL SERVER 2008之后,主库和镜像库之间的日志流传送会默认使用压缩,压缩一方面降低了网络压力,另一方面增大了镜像两端的CPU压力. 可以打开 TF 1462 来关闭日志流压缩 SQL S ...

  9. (2.5)DDL增强功能-触发器trigger

    SQL Server:触发器详解   1. 概述 2. 触发器的分类 3. Inserted和Deleted表 4. 触发器的执行过程 5. 创建触发器 6. 修改触发器: 7. 删除触发器: 8. ...

  10. Underscore.js (1.7.0)-函数预览

    集合(Collections)(25) - each - map - reduce - reduceRight - find - filter - where - findWhere - reject ...