Bank Hacking CodeForces, hackingcodeforces

Source: Internet
Author: User

Bank Hacking CodeForces, hackingcodeforces

Question

Question:

A stupid dog wants to go to the Black bank. There are n Banks, and they are connected by n-1 edges. You can select any bank to start hacking, but each of the subsequent blacklisted banks must be directly connected to a bank that has already been hacked. Each bank initially has a defense value. When a bank is hacked, the Protection Value of the bank that is directly connected to it will be + 1, the protection value of the non-blacklisted bank that is connected directly to the non-blacklisted bank is also + 1 ,. A stupid dog needs to hack all banks, and its computer strength must be equal to the defense value at the moment when all banks are hacked. The minimum Computer strength is required.

Analysis:

Note: when x is hack, the intensity is s [x], which is higher than the original one. If you try it a little, you will find that s [selected for the first time] is 0, s [neighbor selected for the first time] = 1, s [all others] = 2.

Selected S has updated
Select 4 S [4] = 0 s [3] = s [5] = s [2] = s [6] = 1
Select 5 S [6] = 2 s [7] = 1
Select 6 S [7] = 2
Select 3 S [2] = 2 s [1] = 1
Select 7 None
Select 2 S [1] = 2
Select 1 None

So, brute force.

1 # include <cstdio> 2 # include <algorithm> 3 using namespace std; 4 typedef long LL; 5 struct Edge 6 {7 LL to, next; 8} edge [1000100]; 9 LL num_edge, first [500100], d [500100], ans = 0x3f3f3f3f, n; 10 struct Node 11 {12 Node * lc, * rc; 13 LL l, r; 14 LL maxn; 15} ssss [3000100]; // line segment tree, however, no need for 16 LL num_node; 17 Node * getnode () 18 {19 return & ssss [num_node ++]; 20} 21 Node * build (LL l, LL r) 22 {23 Node *_ Tmp = getnode (); 24 _ tmp-> l = l; 25 _ tmp-> r = r; 26 if (l = r) 27 {28 _ tmp-> maxn = d [l]; 29 // _ tmp-> lc = NULL; 30 // _ tmp-> rc = NULL; 31 return _ tmp; 32} 33 LL m = (l + r)> 1; 34 _ tmp-> lc = build (l, m ); 35 _ tmp-> rc = build (m + 1, r); 36 _ tmp-> maxn = max (_ tmp-> lc-> maxn, _ tmp-> rc-> maxn); 37 return _ tmp; 38} 39 LL query (ll l, ll r, Node * p) 40 {41 LL l = p-> l; 42 LL r = p-> r; 43 if (L <= l & r <= R) 44 return p-> maxn; 45 LL ans =-0x3f, m = (L + r)> 1; 46 if (L <= m) ans = max (ans, query (L, R, p-> lc )); 47 if (R> m) ans = max (ans, query (L, R, p-> rc); 48 return ans; 49} 50 void update (ll l, node * p, LL x) 51 {52 LL l = p-> l; 53 LL r = p-> r; 54 if (l = r) 55 {56 // p-> maxn = max (p-> maxn, x); 57 p-> maxn + = x; 58 return; 59} 60 LL m = (l + r)> 1; 61 if (L <= m) update (L, p-> lc, x ); 62 else update (L, p-> rc, x); 63 p-> maxn = max (p-> lc-> maxn, p-> rc-> maxn ); 64} 65 int main () 66 {6 7 LL I, a, B, k; 68 scanf ("% lld", & n); 69 for (I = 1; I <= n; I ++) 70 scanf ("% lld", & d [I]); 71 for (I = 1; I <n; I ++) 72 {73 scanf ("% lld", & a, & B); 74 edge [++ num_edge]. to = B; 75 edge [num_edge]. next = first [a]; 76 first [a] = num_edge; 77 edge [++ num_edge]. to = a; 78 edge [num_edge]. next = first [B]; 79 first [B] = num_edge; 80} 81 Node * x = build (1, n); 82 for (I = 1; I <= n; I ++) 83 {84 update (I, x,-2); 85 k = first [I]; 86 while (k! = 0) 87 {88 update (edge [k]. to, x,-1); 89 k = edge [k]. next; 90} 91 ans = min (ans, query (1, n, x) + 2); 92 update (I, x, 2 ); 93 k = first [I]; 94 while (k! = 0) 95 {96 update (edge [k]. to, x, 1); 97 k = edge [k]. next; 98} 99} 100 printf ("% lld", ans); 101 // del (x); 102 return 0; 103}

Paste another one

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.