Description
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.
解题思路
游戏步骤为:首先任意取一点 hack ,之后以该点为中心以类似 BFS 的形式向周围遍历。
简单模拟一下可以发现,对以任意一点 i 为起点,需要 strength 为 ai ( ai 为 input 中所需 hank 该点的最小 strength)。i 点的所有 neighboring 点,所需 strength 为 a+1 。剩余所有点为 a+2 。
故于最小所需 strength 有关只有 maxStrength=max(a1,a2,...,an) 以及 preStrength=maxStrength−1 。
枚举所有的点作为起点所需的最小 strength 求最小值。对每次枚举:
- 若除点 i 及 i 点的 neighboring 点外仍存在 maxStrength ,则以点为起点的游戏最小 strength 为 maxStrength+2
- 若只有点 i 为 maxStrength :
- 若除点 i 及 i 点的 neighboring 点外存在 preStrength ,则以点为起点的游戏最小 strength 为 maxStrength+1
- 否则,则以点为起点的游戏最小 strength 为 maxStrength
- 若 i 点的 neighboring 点存在 maxStrength ,则以点为起点的游戏最小 strength 为 maxStrength+1
代码
#include<bits/stdc++.h>
using namespace std;
int n, a[300010], cpya[300010], maxStrength = -(1e9 + 7), maxDig, preDig, ans = 1e9 + 7;
struct Edge {
int nxt, to;
} e[300010 * 2];
int head[300010], cnt;
void add(int u, int v)
{
e[++cnt].nxt = head[u];
e[cnt].to = v;
head[u] = cnt;
}
int calNbor(int rt)
{
int rtDig = 0, nbDig = 0, pDig = 0;
for(int i=head[rt], to;i;i=e[i].nxt)
{
to = e[i].to;
if(a[to] == maxStrength)
nbDig++;
else if(a[to] + 1 == maxStrength)
pDig++;
}
if(a[rt] == maxStrength) rtDig++;
else if(a[rt] == maxStrength) pDig++;
if(rtDig + nbDig < maxDig) ans = min(ans, maxStrength + 2);
else if(rtDig + nbDig == maxDig)
{
if(nbDig == 0)
{
if(pDig == preDig) ans = min(ans, maxStrength);
else ans = min(ans, maxStrength+1);
}
else
ans = min(ans, maxStrength + 1);
}
}
int main()
{
scanf("%d", &n);
for(int i=1;i<=n;i++)
scanf("%d", &a[i]), cpya[i] = a[i];
sort(cpya+1, cpya+n+1);
maxStrength = cpya[n];
for(int i=n;i;--i)
{
if(maxStrength == cpya[i]) maxDig++;
if(cpya[i] + 1 == maxStrength) preDig++;
}
for(int i=1, u, v;i<n;i++)
scanf("%d %d",&u,&v),
add(u, v), add(v, u);
for(int i=1;i<=n;i++)
calNbor(i);
printf("%d\n", ans);
}