Codeforces Round #408 (Div. 2) -- C. Bank Hacking(分类讨论)

来源:互联网 发布:淘宝求购区在哪里 编辑:程序博客网 时间:2024/06/06 14:54

题意:

给你一个树状图的银行结点, 你要攻击所有的银行, 每个银行有一个权值wi, 攻击这个银行你需要能力值为wi, 你攻击一个银行,那么这个银行相邻的银行的权值+1,间接相邻 的银行权值也加1, 这里的间接相邻指的是: 如果i 和j 间接相邻,那么必然存在一个没被攻击的k 使得i与k 相邻, k 与j 相邻。

思路:

找找规律便发现:

如果从某个结点i 开始 攻击的话,那么和i 直接相邻的点  需要w+1的能力值, 其余的都是 w+2

那么 我们就可以分类讨论了:

假设所有结点权值最大值是m

1.如果答案是w: 那么只能是只有一个m,并且所有的m-1 与m 相邻。

2.如果答案是w+1:那么必须存在一个点  连接着所有的m,并且必须从这个点开始攻击。

3. 其余情况均是w+2



如果不分类讨论的话 直接拿一个set 模拟这个过程也可以:

#include <bits/stdc++.h>#define Siz(x) (int)x.size()using namespace std;const int maxn = 3e5 + 7;const int oo = 1e9 + 7;int a[maxn];vector<int>g[maxn];int m,n;vector<int>v[2];int solve(){    int sum1 = 0,sum0 = 0;    for (int i = 1; i <= n; ++i){        if (a[i] == m){            v[1].push_back(i);            sum1++;        }        else if (a[i] == m-1){            v[0].push_back(i);            sum0++;        }    }    if (sum1 == 1){        int sum = 0;        int p = v[1][0];        for (int i = 0; i < Siz(g[p]); ++i){            int w = g[p][i];            if (a[w] == m-1) ++sum;        }        if (sum == sum0) return m;        return m+1;    }    bool ok = 0;    for (int i = 1; i <= n; ++i){        int sum = 0;        if (a[i] == m) ++sum;        for (int j = 0; j < Siz(g[i]); ++j){            int w = g[i][j];            if (a[w] == m)++sum;        }        if (sum == sum1) {            ok = 1;            break;        }    }    if (ok) return m + 1;    return m+2;}int main(){    scanf("%d",&n);    m = -oo;    for (int i = 1; i <= n; ++i) {        scanf("%d",a+i);        m = max(m,a[i]);    }    for (int i = 1; i < n; ++i){        int u,v;        scanf("%d %d",&u, &v);        g[u].push_back(v);        g[v].push_back(u);    }    int ans = solve();    printf("%d\n",ans);    return 0;}/**51 2 3 4 51 22 33 44 55738 -29 87 93 39 28 -551 22 53 22 41 77 69351 2 7 6 71 55 33 42 48**/

C. Bank Hacking
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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:

  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.

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 ≤ nui ≠ 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
51 2 3 4 51 22 33 44 5
output
5
input
738 -29 87 93 39 28 -551 22 53 22 41 77 6
output
93
input
51 2 7 6 71 55 33 42 4
output
8
Note

In 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 423157, and 6, in this order. This way, he can hack all banks using a computer with strength 93.



 

0 0
原创粉丝点击