• codeforce 796C


    题目

    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:

    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.

    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 ≤ n, ui ≠ 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 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条边,现在让你任选一个点当做起点,去掉这个点,然后和这个点连接的所有的点的权值都加一,然后所有和那个点相连的点的点的点权也要加一,有点绕,慢慢理解,1->2->3,就是说去掉1后2和3的点权都要加一,然后问你最大集合中的最小点权。

    根据这个易知这是个无根树。
    然后接下来是来一遍预处理。求出各个点中与其相邻Max,Max-1的个数,包含当前节点。接下来设所有点中Max的个数为sum1, Max-1的个数为sum2;

    (1)当答案为Max时,这时得有sum1==1,且当前点的子节点包含了全部的值为Max-1的节点。

    (2)当答案为Max+1时,这时候要有一个节点的子节点包含了所有的值Max的节点。

    (3)剩下的就是答案为Max+2;

    代码

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <vector>
    const int inf = 0x3f3f3f3f;
    const int maxn = 4e5+10;
    using namespace std;
    int a[maxn];
    vector<int >G[maxn];
    int n;
    int solve(){
        int x=0,y=0;
        int maxval=-inf;
        for(int i=1;i<=n;i++){
            if(a[i]>maxval)
                maxval=a[i];
        }
        for(int i=1;i<=n;i++){
            if(a[i]==maxval)
                x++;
            if(a[i]==maxval-1)
                y++;
        }
        int xx=x;
        int yy=y;
        bool flag1=true;
        bool flag2=true;
        for(int i=1;i<=n;i++){
            bool flag3=true;
            if(a[i]==maxval)
                x--;
            else if(a[i]==maxval-1)
                y--;
            for(int j=0;j<(int )G[i].size();j++){
                if(a[G[i][j]]==maxval){
                    x--;
                    flag3=false;
                }
                if(a[G[i][j]]==maxval-1)
                    y--;
            }
            if(x==0){
                flag1=false;
                if(y==0&&flag3)
                    flag2=false;
            }
            x=xx,y=yy;
        }
        if(!flag1){
            if(flag2==0)
                return maxval;
            else
                return maxval+1;
        }
        return maxval+2;
    }
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        for(int i=0;i<n-1;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        int ans=solve();
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    Oracle-DQL 7- 集合操作
    Oracle-DQL 6- 子查询
    Oracle-DQL 5- 分组函数(多行函数)
    Oracle-DQL 4- 多表查询
    Oracle-DQL 3- 单行函数
    构建gulp项目
    重开Vue2.0
    ES6
    emmet简单记录
    webpack 3.X研究
  • 原文地址:https://www.cnblogs.com/Vocanda/p/12809535.html
Copyright © 2020-2023  润新知