1. 程式人生 > >Bank Hacking CodeForces - 796C

Bank Hacking CodeForces - 796C

大於 mar next -1 png ssss query 題意 width

題目

題意:

一條笨狗要去黑銀行,銀行有n個,它們之間用n-1條邊連接。可以選擇任意一個銀行開始黑,但是後面每一次黑的銀行都要求與已經黑過的銀行直接相連。每個銀行初始有一個防禦值,每一個銀行被黑後,與其直接相連的未被黑的銀行的防禦值會+1,與“與其直接相連的未被黑的銀行”相連的未被黑的銀行的防禦值也會+1,。笨狗要黑完所有銀行,且其電腦的強度要求大於等於所有銀行被黑那一刻的防禦值。現在要求電腦的最小強度。

分析:

記點x在被hack時比原來高的強度為s[x], 稍微嘗試一下就會發現,s[第一次選的]=0,s[第一次選的的neighbor]=1,s[其他所有]=2.

選的 s有更新的
選4 s[4]=0 s[3]=s[5]=s[2]=s[6]=1
選5 s[6]=2 s[7]=1
選6 s[7]=2
選3 s[2]=2 s[1]=1
選7
選2 s[1]=2
選1

所以,暴力吧

  1 #include<cstdio>
  2 #include<algorithm>
  3 using namespace std;
  4 typedef long 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];//線段樹,然而根本用不著 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 { 67 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%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 }

再貼一張

技術分享

Bank Hacking CodeForces - 796C