CF-796C
C. Bank Hackingtime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
InputThe 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.
OutputPrint one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
Examplesinput5
1 2 3 4 5
1 2
2 3
3 4
4 5output5input7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6output93input5
1 2 7 6 7
1 5
5 3
3 4
2 4output8NoteIn 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家银行,每个银行都有自己的权值,当我们攻击一个银行时,跟他距离为1和2的银行权值都会+1。
只有在我们本身权值大于银行权值时才可以攻击,求本身至少需要多少权值。
可以将所有的银行关系看成一棵树,则与根节点距离为一的银行最终会+1,其余的+2.
由此我们就有maxn,maxn+1,maxn+2三种可能的结果。
当最大值maxn只有一个时,若所有maxn-1距离maxn都为1,则本身需要maxn,否则需要maxn+1。
当最大值大于一个时,若存在一点,其本身和距离为1的点包含了所有maxn,则只需要maxn+1,
否则maxn+2。
附AC代码:
#include<bits/stdc++.h>
using namespace std; const int N=; vector<int> mp[N];
int a[N]; int main(){
int n,x,y,u,ans;
int maxn=-;
cin>>n;
for(int i=;i<=n;i++){
mp[i].clear();
cin>>a[i];
maxn=max(a[i],maxn);
}
for(int i=;i<n;i++){
cin>>x>>y;
mp[x].push_back(y);
mp[y].push_back(x);
}
int ma=,mb=;
for(int i=;i<=n;i++){
if(a[i]==maxn)
ma++,u=i;
if(a[i]==maxn-)
mb++;
}
if(ma==){
int cont=;
for(int i=;i<mp[u].size();i++){
int v=mp[u][i];
if(a[v]==maxn-)
cont++;
}
if(cont==mb)
ans=maxn;
else
ans=maxn+;
cout<<ans<<endl;
}
else{
bool flag=false;
for(int i=;i<=n;i++){
int cont=;
if(a[i]==maxn)
cont++;
for(int j=;j<mp[i].size();j++){
int v=mp[i][j];
if(a[v]==maxn)
cont++;
}
if(cont==ma)
flag=true;
}
if(flag){
cout<<maxn+<<endl;
}
else{
cout<<maxn+<<endl;
}
}
return ;
}
CF-796C的更多相关文章
- 【模拟】CF 796C Bank Hacking
题目大意 洛谷链接 给定一棵带点权树,选出一个最佳的根节点,使得根节点的点权不变,它的儿子点权加1,其余点点权加2,并使最大点权最小,输出这个最小的最大点权. 其他见链接(懒). PS:原题面很不好总 ...
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
随机推荐
- bootstrap到底是用来做什么的(概念)
Bootstrap官网:http://v3.bootcss.com/ Bootstrap是Twitter推出的一个用于前端开发的开源工具包.它由Twitter的设计师Mark Otto和Jacob T ...
- Ffmpeg 实现文件切割
文件切割是一项很常见的基本功能,通过Ffmpeg可以很容易实现这项功能. 首先介绍下基本原理,文件切割说白了就过滤掉文件的部分音视频包,按照什么规则过滤呢? 答案是时间戳.文件中每个视频及音频包都有时 ...
- WPF简单计算器
- 内核顶层Makefile相关2
http://www.groad.net/bbs/simple/?f104.html if 函数 if 函数的语法有两种形式: () $(if <condition>, <then ...
- Android开发之裁切(拍照+相冊)图像并设置头像小结
先看效果: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...
- Oracle SQL_杂记
1. 查询当前用户角色,当前用户下的表权限以及所有用户表权限 desc user_role_privs;select * from user_role_privs; desc user_tab_pri ...
- Android API Guides---NFC Basics
本文档介绍了Android中运行基本任务NFC. 它说明了怎样在NDEF消息的形式发送和接收数据的NFC并介绍了支持这些功能的Android框架的API. 对于更高级的主题.包含与非NDEF数据工 ...
- Liquibase
http://www.liquibase.org/documentation/index.html https://github.com/studygolang/studygolang/tree/ma ...
- Java版TicTacToe
MainFrame.java package com.bu_ish; import java.awt.BorderLayout; import java.awt.Color; import java. ...
- RequestDispatcher用法
1.具体用法: RequestDispatcher dispatcher = req.getRequestDispatcher("updateByUserId_001.jsp"); ...
