Bzoj 2525 [poi2011]dynamite two points + tree-type greedy

Source: Internet
Author: User

Test Instructions:
n points, a tree, some points are key points, can be M-Dianran color.
Minimize the maximum distance from all key points to the nearest dyed point.
parsing:
Anyway, from this problem I have learned a kind of idea of doing the problem?
Grandpa said when the lecture: The general choice of some points of the same price is greedy, the price of different words are generally DP.
Think is also quite right, but is not aware of it?
Anyway, this exam I was direct D---!
Forget why D.
Obviously the maximum value is minimal. We need two minutes for this value.
Then we sweep the whole tree from the bottom up.
How many nodes are there in the state?
The first is that there are no key points within the subtree that are not overwritten, and the contribution of one node in the subtree can continue upward.
The second is that there are key points in the subtree that are not covered, and the contribution of the subtree without nodes can be continued upward.
The third is that there are no key points to be overwritten, and there is no point that can continue to contribute.
The fourth type is all there.
But we can prove that the fourth case exists, obviously can pick a point outside the subtree to cover the key is not covered, but this time the sub-tree in the selection of the point is not used, so this situation can be attributed to the third kind.
Then it is the process of greedy discussion.
The greedy thought is that you can dye it without dyeing it.
Obviously the first one we need to keep track of how much we can go up.
The second is the distance we need to record the farthest non-covered key points to reach the current root node

I think the way to do something is more important.
That's still the word.
the common choice of some points of the same price is greedy, the price of the different words are generally DP.
Perhaps this sentence will solve my future greed and DP trouble? 2333
Code:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 300100using namespace STD;intN,m;intFlag[n];intHead[n];intStatus[n];intDocument[n];intCntintTotstructnode{intFrom,to,next;} edge[n<<1];voidInit () {memset(head,-1,sizeof(head)); Cnt=1;}voidEdgeadd (intFromintTo) {edge[cnt].from=from,edge[cnt].to=to;    Edge[cnt].next=head[from]; head[from]=cnt++;}//0-> Continue delivery//1-> has not been overwritten//2-> neither delivered nor not overwritten. voidDfsintNowintFaintDIS) {intuncovered=flag[now]-1;intnear=-1; for(inti=head[now];i!=-1; i=edge[i].next) {intto=edge[i].to;if(TO==FA)Continue;    DFS (To,now,dis); } for(inti=head[now];i!=-1; i=edge[i].next) {intto=edge[i].to;if(TO==FA)Continue;if(status[to]==0) Near=max (near,document[to]-1);Else if(status[to]==1) Uncovered=max (uncovered,document[to]+1); }if(near<uncovered) {if(Uncovered==dis) document[now]=dis,status[now]=0, tot++;Elsedocument[now]=uncovered,status[now]=1; }Else if(near!=-1) document[now]=near,status[now]=0;Elsestatus[now]=2, document[now]=0;}BOOLCheckintdis) {tot=0; Dfs1,0, dis);if(status[1]==1) tot++;returnTot<=m?1:0;}intMain () {init ();scanf("%d%d", &n,&m); for(intI=1; i<=n;i++)scanf("%d", &flag[i]); for(intI=1; i<n;i++) {intx, y;scanf("%d%d", &x,&y);        Edgeadd (x, y);    Edgeadd (Y,X); }intL=0, R=n,ans; while(L&LT;=R) {intMid= (l+r) >>1;if(Check (mid)) ans=mid,r=mid-1;ElseL=mid+1; }printf("%d\n", ans);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 2525 [poi2011]dynamite binary + Tree greedy

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.