Codeforces Round #408 (Div. 2) C. Bank Hacking
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:
- 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.
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.
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的更多相关文章
- Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)
题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
- Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)
传送门 题意 给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring, 每家银行hack的难度为 ...
- 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 ...
- Codeforces Round #408 (Div. 2)
C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...
- Codeforces Round #408 (Div. 2) 题解【ABCDE】
A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...
- Codeforces Round #408 (Div. 2) C
Description Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. ...
- 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 ...
- Codeforces Round #408 (Div. 2) D - Police Stations
地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...
- 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 ...
随机推荐
- 【BZOJ4282】慎二的随机数列 乱搞
[BZOJ4282]慎二的随机数列 Description 间桐慎二是间桐家著名的废柴,有一天,他在学校随机了一组随机数列, 准备使用他那强大的人工智能求出其最长上升子序列,但是天有不测风云,人有旦夕 ...
- 最小费用流判负环消圈算法(poj2175)
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3384 Accepted: 888 ...
- 基础概念 之 Hadoop Family
Hadoop家族的技术,网上资料多如牛毛,但是还是那句老话——好脑瓜不如烂笔头,看的再多也不如自己动手写一写. Hadoop是一个分布式系统,有两个关键组件——HDFS和MapReduce,HDFS负 ...
- Linux定时对日志批量打包Shell脚本及定时任务crontab 详细用法
一.需求背景 因此次项目的生产环境中部署了多套系统,每天会产生大量的日志(数百GB的量),侵占了服务器宝贵的存储资源空间.为了有效缓解服务器存储压力,考虑通过Linux的Shell脚本结合cr ...
- Spring项目对JDBC的支持和基本使用
欢迎查看Java开发之上帝之眼系列教程,如果您正在为Java后端庞大的体系所困扰,如果您正在为各种繁出不穷的技术和各种框架所迷茫,那么本系列文章将带您窥探Java庞大的体系.本系列教程希望您能站在上帝 ...
- oracle日常函数汇总(转载)
第一篇 著名函数之单值函数 注:N表示数字型,C表示字符型,D表示日期型,[]表示内中参数可被忽略,fmt表示格式 数值类型函数 数值型函数输入数字型参数并返回数值型的值.多数该类函数的返回值支持38 ...
- StartUML-时序图
- Python开发【数据结构】:算法(二)
堆排序 1.—树与二叉树简介 树是一种数据结构 比如:目录结构 树是一种可以递归定义的数据结构 树是由n个节点组成的集合: 如果n=0,那这是一棵空树: 如果n>0,那存在1个节点作为树的根节点 ...
- Mirror--镜像断开的解决办法
如果镜像在搭建一段时候后出现问题,可能存在以下原因: 1. 因为主库或镜像库存在内存压力,导致无法完成镜像日志传送和重做 解决办法:设置数据库最小内存,保证数据库有足够内存完成镜像操作 2. 因为主库 ...
- thinkcmf安装教程与目录结构详解 快速上手版
最近接了一个建站项目,要求用thinkcmf来搭建,ytkah在想php都大致一样吧,快速地下载安装包,可是!怎么安装呢?没看到安装指引文件或目录,查看了安装说明public目录做为网站根目录,入口文 ...