Codeforces Round #408 (Div. 2) C. Bank Hacking

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 neighboringand 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
5
1 2 3 4 5
1 2
2 3
3 4
4 5
output
5
input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
output
93
input
5
1 2 7 6 7
1 5
5 3
3 4
2 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.

题目大意:有n个银行,同时用n-1个线连起来,同时每个银行有一个安保力度,每次选择了一个银行抢钱,有关他直接相连的银行安保力度都会+1,然后与他直接相连的银行直接相连的银行也+1(就是半相连,通过与他相连的银行连接她的),第一次抢银行可以随机抢,之后只能选择已经被抢的银行所连接的银行
解题思路:一开始没有读出来,只有第一次抢银行是选择,之后是按照一定规矩,所以WA了一发,询问过了之后,找人讨论了一下,发现了一个解题思路,就是你只需要判断最大数和最大数-1的连接情况就可以解决了,并且在你一开始选的那个点后,直接连接的两个点是+1,剩下的点都是+2,所以这道题的结果无非是最大数,最大数+1,最大数+2的情况.
最大数的原因是,只有一个最大值且最大数-1的点全部与最大数的点直接相连
最大数+1的原因是,存在一个点,使得所有最大数的点都与之相连或者只有一个最大值的时候,并不是所有的最大数-1都与之相连
最大数+2是上面情况之外的情况都是
#include<iostream>    
#include<cstdio>  
#include<stdio.h>  
#include<cstring>    
#include<cstdio>    
#include<climits>    
#include<cmath>   
#include<vector>  
#include <bitset>  
#include<algorithm>    
#include <queue>  
#include<map>  
using namespace std;

struct BANK
{
	long long int strengths;
	int x;
}bank[300005];
vector<int> tu[300005];
long long int n, ma, x, y, ans1, ans2, l, k, a[300005], b[300005];
int i, j;
int main()
{
	cin >> n;
	ans1 = ans2 = 0;
	ma = -1e9 - 1;
	for (i = 1; i <= n; i++)
	{
		cin >> bank[i].strengths;
		ma = max(ma, bank[i].strengths);
		bank[i].x = i;
	}
	for (i = 1; i <= n - 1; i++)
	{
		cin >> x >> y;
		tu[x].push_back(y);
		tu[y].push_back(x);
	}
	for (i = 1; i <= n; i++)
	{
		if (bank[i].strengths == ma)
		{
			ans1++;
			a[ans1] = bank[i].x;
		}
		if (bank[i].strengths == ma - 1)
		{
			ans2++;
			b[ans2] = bank[i].x;
		}
	}
	l = 0;
	if (ans1 == 1)
	{
		for (i = 0; i < tu[a[ans1]].size(); i++)
		{
			k = tu[a[ans1]][i];
			if (bank[k].strengths == ma - 1)
			{
				l++;
			}
		}
		if (ans2 == l)
		{
			cout << ma << endl;
			return 0;
		}
		else
		{
			cout << ma + 1 << endl;
			return 0;
		}
	}
	bool flag = false;
	for (i = 1; i <= n; i++)
	{
		l = 0;
		if (bank[i].strengths == ma) l++;
		for (j = 0; j < tu[i].size(); j++)
		{
			k = tu[i][j];
			if (bank[k].strengths == ma)
			{
				l++;
			}
		}
		if (l == ans1)
		{
			flag = true;
			break;
		}
	}
	if (flag == true)
	{
		cout << ma + 1 << endl;
		return 0;
	}
	else
	{
		cout << ma + 2 << endl;
		return 0;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值