CodeForces 796C Bank Hacking
- 题目描述
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.
- 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?≤?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.
- Output
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
- Examples
Input 1
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output 1
5
Input 2
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output 2
93
Input 3
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output 3
8
- Tips:
题意:有一张无向图,每个节点有一个权值,现在要让选择的节点的边断开,并且相邻节点和距离为2的节点的权值加一。第一次选择可以选择任意一个节点(称为被hack),之后必须选择已经有边被断开的节点,且选择的节点的权值要小于自己的strength。问把所有节点的边断开至少需要多少strength。
因为除了第一次以外,只能选已经被hack节点旁边的节点,所以问题可以转化为,选择一个节点,它相邻的节点权值+1,其余节点权值+2。那么:
设所有节点权值最大值为maxn, 最大权值节点有mx个, 权值为
maxn - 1的点有mc个,则
- ans = maxn
此时,mx == 1且权值为maxn - 1的节点全在权值最大的值旁边- ans = maxn + 1
此时,
(1) mx == 1且存在权值为maxn - 1的节点不在最大权值点旁边
(2)
mx != 1 且存在一个点,所有权值为maxn的点都在他旁边- ans = maxn + 2
其余情况
- Answer
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int SIZE = 3e5 + 5;
int strength[SIZE];
vector<int> a[SIZE];
int n;
int maxn = -1e9-5, mx = 0, mc = 0;
//check()判断是否存在一个节点,所有最大值点都在他的旁边
int check(void){
//遍历每个节点
for(int i = 1; i <= n; i++){
//初始化count
int count = 0;
//假如这个节点本身是个最大值点
if(strength[i] == maxn) count++;
//遍历这个节点周围的每个节点
for(int j = 0; j < a[i].size(); j++){
if(strength[a[i][j]] == maxn){
count++;
}
}
//存在一个点,所有最大值点都在他的旁边
if(count == mx) return 1;
}
return 0;
}
int main(void){
int ans;
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> strength[i];
}
//构造树
for(int i = 1; i<= n - 1; ++i){
int t1, t2;
cin >> t1 >> t2;
a[t1].push_back(t2);
a[t2].push_back(t1);
}
int address;
//求maxn,mx(maxn个数)
for(int i = 1; i <= n; ++i){
if(strength[i] > maxn){
maxn = strength[i];
address = i;
mx = 1;
}
else if(strength[i] == maxn){
mx++;
}
}
//求mc(maxn-1个数)
for(int i = 1; i <= n; i++){
if(strength[i] == maxn - 1){
mc++;
}
}
//cnt=maxn点旁边有多少个maxn-1点
int cnt = 0;
for(int i = 0; i < a[address].size(); i++){
if(strength[a[address][i]] == maxn - 1){
cnt++;
}
}
//只有一个最大值点
if(mx == 1){
//mx==1并且maxn旁边有mc个maxn-1点
if(cnt == mc){
ans = maxn;
}
//存在maxn-1点不在maxn旁边
else{
ans = maxn + 1;
}
}
//有多个最大值点
else{
//有一个点旁边所有最大值都连着它
if(check()){
ans = maxn + 1;
}
else{
ans = maxn + 2;
}
}
cout << ans << endl;
return 0;
}