zoukankan      html  css  js  c++  java
  • bank hacking 思维 multiset

    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.

    img

    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 a i.

    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 a 1, a 2, ..., a n ( - 109 ≤ a i ≤ 109) — the strengths of the banks.

    Each of the next n - 1 lines contains two integers u i and v i (1 ≤ u i, v i ≤ n, u i ≠ v i) — meaning that there is a wire directly connecting banks u i and v i.

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

    题意

    题意:n个点,n-1条边组成的树,每个点价值为a[i],如果两点有边直接相连,这两点算相邻,如果存在三个点i,j,kik相邻,且 j也与k相邻则ij算半相邻。

    刚开始,每个点都在线状态,你有一个初始值x(待求),可以选择任意一点i(a[i]<=x)进行攻击,攻击i后,和i相邻和半相邻的点a[]++,同时i处于下线状态,要求攻击n个点需要的最小x?

    除了第一次外,每次攻击的点i必须满足:

    1. i必须在线
    2. i必须和某个处于离线状态的点相邻,
    3. a[i]<=x

    分析

    我们每次都选择一个点rt作为树的根节点,那么rt的权值不变,和rt直接相连的点的权值会加1,剩余的点的权值都会加2。举个例子

    我们把1作为根节点,首先把1除去,2,3与1直接相邻,4与1半相邻,234的权值都加1

    把2除去,其他点权值不变

    把3除去,4与3直接相邻,5,6与3半相邻,456的权值都加1

    把4除去,56的权值加1

    把5除去,其他点权值不变

    把6除去,其他点权值不变

    综上,1的权值没变,23的权值加了1, 456的权值加了2.

    由此得到刚才的结论。这个题我们可以利用multiset来存每个点的权值,遍历1~n,每次删除第i个点和与i直接相邻的点,比较i的权值,与i直接相邻的点的权值加1,以及剩余的点中的最大权值+2,每次取min,再把删除的点重新放回multiset即可。

    /*************************************************************************
    	> File Name: f.cpp
    	> Author: LiuGeXian
    	> Mail: 1019630230@qq.com 
    	> Created Time: 2020/4/30 12:19:45
     ************************************************************************/
    
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 3e5 + 5;
    struct Node{
    	int to, next;
    }edge[maxn << 1];
    int n, head[maxn], cnt, a[maxn];
    int ans = 0x7fffffff;
    multiset<int> s;
    void Add(int u, int v){
    	edge[++cnt].to = v;
    	edge[cnt].next = head[u];
    	head[u] = cnt;
    }
    int main(){
    	scanf("%d", &n);
    	for (int i = 1; i <= n; i++){
    		scanf("%d", &a[i]);
    		s.insert(a[i]);
    	}
    	for (int i = 1; i < n; i++){
    		int u, v;
    		scanf("%d%d", &u, &v);
    		Add(u, v);
    		Add(v, u);
    	}
    	multiset<int>:: iterator it;
    	for (int i = 1; i <= n; i++){
    		int temp = a[i];
    		s.erase(s.find(a[i]));//把a[i]除去
    		//遍历和i直接相邻的点
    		for (int j = head[i]; j; j = edge[j].next){
    			int v = edge[j].to;
    			temp = max(temp, a[v] + 1);//第i个点和第v个点的权值加1的大小
    			s.erase(s.find(a[v]));//把a[v]除去
    		}
    		//直接找到剩余的点中权值最大的
    		if (s.size()){
    			it = s.end();
    			temp = max(temp, *(--it) + 2);//end()返回的是最后一个元素的下一个位置
    									     //it需要先--
    		}
    		//再把刚才除去的点加入
    		s.insert(a[i]);
    		for (int j = head[i]; j; j = edge[j].next){
    			int v = edge[j].to;
    			s.insert(a[v]);
    		}
    		ans = min(temp, ans);//取最小值
    	}
    	cout << ans;
    	return 0;
    }
    
    
  • 相关阅读:
    jquery map.js
    json序列指定名称
    如何将后台传来的json反序列化为前端具体对象
    创建随机码!!
    用户(三次)登录--作业小编完成
    求出1-2+3-4+5------100求和
    if -else 条件语句原理
    联系:中奖彩票小编译
    求出1-100内所有奇数。
    练习题:求1-100所有数偶数
  • 原文地址:https://www.cnblogs.com/ghosh/p/12809376.html
Copyright © 2011-2022 走看看