POJ 2054 Color a Tree

Source: Internet
Author: User
Tags new set

Very interesting a greedy problem, I did not think out, understand the other people's thinking

Reference: http://www.cnblogs.com/yu-chao/archive/2012/02/19/2358565.html

http://my.oschina.net/locusxt/blog/210536

The main idea: to give you a tree, each node in the tree has its own weight, now to the tree all the nodes to color, each dye a node needs a unit of time, the cost of dyeing a point is the weight of the point multiplied by the current time (time starting from 1). Before dyeing the current point, it is necessary to dye his father's node to find the minimum cost.

Sample Analysis

Sample Input

5 11 2 1 2 41 21 32 43 5

Indicates that the tree has 5 nodes with a root node of 1. The next 5 numbers represent the weights for each point, and the next n-1 line has two digits v1,v2, representing V1 as the V2 Father node.

idea: Just get this question, I have no train of thought, because each dye a point before must dye his father node first, so the situation is very complicated. Later read the report of others, found that the problem of greedy strategy is very special
First, each point belongs to a collection, with the attributes of each point set being: 1, the number of points in the point set, hereinafter referred to as Nodenum[i]. 2, the point concentration of a bit of the value of the and, hereinafter referred to as Fact[i]. 3. Now introduce a new variable val[i],val[i]=fact[i]/nodenum[i] The greedy strategy is to dye each point with the lowest Val value.

The question now is how to dye this point, in other words, how to find the answer.
Now take the example to illustrate:

(1) First obtain the set 5, because to dye the parent node first, so the 5 into the point set 3, so as to form a new point set is recorded as a set {3,5}, the property of a collection is updated
Ans+=nodenum[3]*fact[5]=4 (This guarantees the time taken to take into account the points that were dyed before 5)
     a set property is updated to Nodenum[a]=2,fact[a]=4+1=5 VAL[A]=5.0/2;
(2) Next get set A, for a, set a is merged into set 1, so as to form a new point set b{1,3,5}
Ans+=nodenum[1]*fact[a]=9
The B collection property is updated to NODENUM[B]=3,FACT[B]=6,VAL[B]=6/3;
(3) then get set 2, the same 2 merge into B after the formation of a new set c{1,3,5,2}
Ans+=nodenum[b]*fact[2]=15
The C collection property is updated to nodenum[c]=4,fact[c]=8,val[c]=2
(4) Acquiring a collection of bis and merging into C to form a new set d{1,3,5,2,4}
Ans+=nodenum[c]*fact[4]=23
nodenum[d]=5,fact[d]=10
Obviously, each node dyeing time is 1 less than the actual time, so the final should be added back, that is ans+=fact[d]=33

So why is it right to do so? According to the reference, simple proof:
To prove that the result is correct, is to prove: for Fact[i]/nodenum[i] (hereinafter referred to as the average weight) the largest point, once his father node staining, he should be immediately dyed.
It may be assumed that the maximum mean weight point is A,a's father node is B, if after B is dyed, A is not immediately dyed, that is, the dyeing sequence is ... b x1 x2 ... xn a ...
That is, after B has been dyed, a is dyed, and a number of nodes have been dyed. Obviously, if we can prove that the cost of a and n swap position is less than the non-exchange position, then it can be proved that a should be dyed immediately after B is dyed. Assuming the total weight value of the set Xn Wxn, the points dxn, the total weight of set A is WA, and the points are da.
Since Wa/da > Wxn/dxn, obviously, the wxn*da-wa*dxn<0, that is, after the exchange position, the cost will be reduced, the proof.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;intc[ .],vis[ .],ANS,DEP;intN,r;intcnt,jihe[ .];structnode{intFA;} nod[ .];structset{intNodenum; DoubleVal; intfact;} ji[ .];voidInitial () { for(intI=1; i<=n;i++) {Ji[i].nodenum=1; Ji[i].val=B[i]; Ji[i].fact=C[i]; NOD[I].FA=i; }}voidUpdateintu) {    if(u==R) {Vis[u]=1; return ; }    Else{Vis[u]=1; intv=Nod[u].fa; if(ji[v].nodenum==1) {CNT++; if(jihe[u]!=0){                 for(intI=1; i<=n;i++){                    if(I!=u && jihe[i]==Jihe[u]) {Jihe[i]=CNT; }}} Jihe[u]=CNT; JIHE[V]=CNT; }        Else{            if(jihe[u]!=0){                 for(intI=1; i<=n;i++){                    if(I!=u && jihe[i]==Jihe[u]) {Jihe[i]=Jihe[v]; }}} Jihe[u]=Jihe[v]; } ans+=ji[u].fact*Ji[v].nodenum; Ji[v].nodenum+=Ji[u].nodenum; Ji[v].fact+=ji[u].fact; Ji[v].val=1.0*ji[v].fact/(1.0*ji[v].nodenum);  for(intI=1; i<=n;i++){            if(jihe[i]==Jihe[v]) {Ji[i]=Ji[v]; }        }        //cout<< "U" <<u<< "V" <<v<< "fact" <<ji[v].fact<< "Val" <<ji[v].val << "ans" <<ans<<endl;    }}intMain () { while(~SCANF ("%d%d", &n,&r) && (n!=0|| r!=0)){         for(intI=1; i<=n;i++) {scanf ("%d",&C[i]);        } Initial ();  for(intI=0; i<n-1; i++){            intV1,v2; scanf ("%d%d",&v1,&v2); NOD[V2].FA=v1; } memset (Vis,0,sizeof(VIS)); Ans=0; ints; CNT=0; memset (Jihe,0,sizeof(Jihe));  for(intI=1; i<=n;i++){            intmaxi,flag=0; Doublemaxn=0;  for(intj=1; j<=n;j++){                if(!Vis[j]) {                    if(ji[j].val>MAXN) {Maxi=J; MAXN=Ji[j].val; Flag=1; }                }            }            if(!flag) {                 Break; } s=Maxi; //cout<< "s" <<s<<endl;Update (maxi); } ans+=ji[s].fact; cout<<ans<<Endl; }    return 0;}
View Code



POJ 2054 Color a Tree

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.