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 ...
随机推荐
- c# SQL Server数据库操作-管理命令参数的类:SqlParameter
使用SqlCommand类来执行Transact-SQL语句或存储过程时,有时需要用参数传值或作为返回值,SqlParameter类正是为了此需要而设计的类.下面介绍如何使用该类为SqlCommand ...
- 【MySQL案例】error.log的Warning:If a crash happens thisconfiguration does not guarantee that the relay lo(转)
标签: 1.1.1. If a crash happens thisconfiguration does not guarantee that the relay log info will be c ...
- Floyd求最小环并求不同最小环的个数
FZU2090 旅行社的烦恼 Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u [Subm ...
- 【数组】—冒泡排序&&选择排序---【巷子】
/* 什么是冒泡排序:从头到尾比较相邻的两个数的大小,如果符合条件则进行比较 [注]:从小到大进行排序 假设有一个数组 var arr = [9,8,7,6,5,4]; 我们想要进行这个数组进行排序那 ...
- 170503、centos6.5安装mysql5.6.30
准备:虚拟机地址:192.168.0.110 安装目录/usr/local/ 首先卸载已经安装的mysql使用命令查看rpm -qa | grep mysql卸载使用 rpm -e xxx 或者 yu ...
- 关于string的length
在C++里面,std::string的length()返回的是字节数,与编码方式有关. int main() { std::string s = "我是中国人"; std::cou ...
- Oracle等待事件之Enqueue(锁)
1.产生原因 表示与内部队列机制相关的等待,例如对保护内部资源或者组件的锁的请求等,一种并发的保护机制,该等待意味着在访问同样的数据库资源时需要等待其他会话已经获取的锁. 2.确定产生问题的对象 死锁 ...
- 知乎live 我的读书经验 总结
https://www.zhihu.com/lives/757587093366009856/messages 碎片化阅读没有意义, 捡硬币捡成富翁 kindle不能全文检索 短篇文章的阅读是否有 ...
- Java 算法-快速幂
1 什么是快速幂? 快速幂,顾名思义就是快速的求次幂,例如:a^b,普通的算法就是累乘,这样的计算方法的时间复杂度就是O(n),而快速幂的方法使得次幂的计算方法的时间复杂度降低到O(logn). 假 ...
- Git学习-->GitLab如何屏蔽掉注册功能?
一.背景 最近发现我在公司内部搭建的GitLab平台上关于账号出现了以下几个问题: 莫名其妙出现很多陌生人的账号 团队成员的很多账户注册填写的Email和UserName都不符合规范 本来一开始是打算 ...