Codeforces Round #408 C. Bank Hacking

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:

  1. Bank x is online. That is, bank x is not hacked yet.
  2. Bank x is neighboring to some offline bank.
  3. 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=maxStrength1

枚举所有的点作为起点所需的最小 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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值