zoukankan      html  css  js  c++  java
  • Codeforces Round #408 (Div. 2) C

    地址:http://codeforces.com/contest/796/problem/C

    题目:

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

    思路:昨晚一直忘了这是棵树,然后搞了半天还是做不出,mdzz

      因为每个点的值只能加0(根节点),加1(根节点的neighboring ),加2(除了以上点外的所有点)。(因为这是一棵数!!!)

      所以答案只有mx或mx+1或mx+2 (mx为v[i]中的最大值)。

      记v[i]==mx的点有suma个,v[i]==mx-1的点有sumb个。

      然后直接枚举每个点作为根时(假设选的根为x),统计他的neighboring :v[i]为mx的点有cnta个,v[i]为mx-1的点有cntb个。

      然后分类讨论:

      1.当cnta+(v[i]==mx)!=suma时,有v[i]==mx的点权值加2了,所以ans等于mx+2

      2.当cnta||cntb+(v[i]==mx-1)!=sumb时,有v[i]==mx的点权值加1或者有v[i]==mx-1的点权值加2了,所以ans等于mx+1

      3.ans=mx

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 vector<int>mp[K];
    15 int n,v[K],mx=-1e9-5,suma,sumb,ans=1e9+7;
    16 int main(void)
    17 {
    18     cin>>n;
    19     for(int i=1;i<=n;i++)
    20         scanf("%lld",v+i),mx=max(mx,v[i]);
    21     for(int i=1,x,y;i<n;i++)
    22         scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
    23     for(int i=1;i<=n;i++)
    24     if(mx==v[i])    suma++;
    25     else if(mx-1==v[i]) sumb++;
    26     for(int i=1;i<=n;i++)
    27     {
    28         int cnta=0,cntb=0;
    29         for(int j=0;j<mp[i].size();j++)
    30         {
    31             if(v[mp[i][j]]==mx)
    32                 cnta++;
    33             else if(v[mp[i][j]]==mx-1)
    34                 cntb++;
    35         }
    36         if(cnta+(v[i]==mx)!=suma)
    37             ans=min(ans,mx+2);
    38         else if(cnta||cntb+(v[i]==mx-1)!=sumb)
    39             ans=min(ans,mx+1);
    40         else
    41             ans=min(ans,mx);
    42     }
    43     cout<<ans<<endl;
    44     return 0;
    45 }
  • 相关阅读:
    ArrayList源码剖析
    Qt线程外使用Sleep
    malloc、calloc和realloc比较
    C++各大名库
    Qt 编译boost
    VC++ 设置控件显示文本的前景色、背景色以及字体
    std::map的操作:插入、修改、删除和遍历
    time.h文件中包含的几个函数使用时须注意事项
    赋值操作符和拷贝构造函数
    virtual析构函数的作用
  • 原文地址:https://www.cnblogs.com/weeping/p/6693013.html
Copyright © 2011-2022 走看看