POJ 2054 Color A tree# greedy (difficult, good question)

Source: Internet
Author: User

Topic links

Code reference this blog: http://www.cnblogs.com/vongang/archive/2011/08/19/2146070.html

Which about Max{c[fa]/t[fa]} Greedy principle , this bo has a good explanation: http://www.cnblogs.com/rainydays/p/3271277.html

Here are a few words to quote:

Imagine, if there is no parent node before the limit of the node, then the problem is very simple, just need to set the node in the weight from the largest to the small arrangement. With this restriction, if the highest-weighted node satisfies the condition (the parent node is placed in a previous position), then the node with the largest weight must be next to the parent node, which is where the most weighted node is ranked at the front of the row. Because for this node if unrestricted should be in the first place, and there is a limit, after meeting the limit should be as far as possible to the front. So it must be next to the parent node. So now in the final permutation we have identified the front and back relationships of two nodes, and bind them together.

Imagine what we should do if we were to keep the adjacent relationship and remove the constraints of the other nodes. We assume that the two nodes that are bound together are A and B. Now that we have an additional node x, we look at how the two permutations of XAB,ABX affect the results of the final calculation. x*i+a* (i+1) +b* (i+2); A*i + b* (i+1) + x* (i+2). The latter minus the former equals 2x-(A+b). After X is moved from AB to AB, AB shifts 1 bits to the left, resulting in a decrease in a+b. X right Shift 2-bit result increased by 2x. So the two who are in front of us only need to compare a+b and 2x and can also compare (A+B)/2 and X.

The generalization of this theorem, bound together is not necessarily two nodes, can be a longer sequence, compared with this sequence to see who placed in front of can also be a sequence. Set a sequence with N1 nodes and a second sequence with N2 nodes. Then we compare the two who put in front of the time need to compare is (n1 the sum of Weights and XN2) and (N2 of Weights and XN1). That is, the change in results from left and right shifts. It is also possible to compare (N1 the sum of/n1) and (N2 of Weights and/n2).

We can do it again, if we're not going to arrange the nodes, but a lot of sequences, then we just need to calculate the average of the weights of each sequence (for example: N-node sequence, to calculate N-Weights and/N), and then the average from large to small can be calculated to minimize the results. This allows the sequence and the node to have a uniform measure-the average number.

In this way, we can consider the operation of the above bound two nodes as an operation to reduce the size of the problem, while we are helping to set the two nodes together, we also merge the two nodes in the tree into a node, the child of the child node becomes the children of the parent node. Then the weighted value of the merged node is the average of the weights of all the nodes that are merged in this node. We succeeded in reducing the size of the problem by 1. Just keep doing this to reduce the problem to just one node.

The following is the AC code:

//greedy: If there is a maximum weight point x (non-root) in this tree,//so once X's parent node y is dyed, it should be dyed x immediately.//so x and Y are combined into a single point set,//weights for new Point sets = (weights and values for all points in the new point set)/(number of points in new point set)//Similarly, the point set can be viewed as a point//repeat the greedy idea until only one root R point set is left#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>using namespacestd;Const intn=1005;structnode{intF//parent Node    intT//Time    intC//Original Weight value    DoubleW//Greedy weighted value (c/t)}node[n];intN,r;intFindpos () {intPOS; Doublewmax=0;  for(intI=1; i<=n;i++)        if(node[i].w>wmax&&i!=r) {Wmax=NODE[I].W; POS=i; }    returnPos;}intMain () { while(SCANF ("%d%d", &n,&r) &&n+r) {intpos,fa,res=0;  for(intI=1; i<=n;i++) {scanf ("%d",&node[i].c); NODE[I].W=NODE[I].C;//The greedy weight w is initialized to the original weight of the value Cnode[i].t=1;//time is initialized to 1RES+=NODE[I].C;//The result is initialized to sum of all the original weights C        }        intu,v;  for(intI=1; i<n;i++) {scanf ("%d%d",&u,&v); NODE[V].F=u;//Record parent Node        }         for(intI=1; i<n;i++) {pos=findpos ();//find the place where the greedy weights are greatest.node[pos].w=0;//0 so that you can skip it the next time you look upFA=NODE[POS].F;//FA is the parent node of POSres+=node[pos].c*node[fa].t;//Res+=pos The original weight value *FA the parent node time (node[fa].t actually represents a few points in the point set)             for(intj=1; j<=n;j++)                if(node[j].f==POS) node[j].f=FA;//if the parent node of J is POS, the parent node of J is changed to FA, and a new collection is established .node[fa].t+=node[pos].t;//update the number of points within a point set with the parent node in FA (that is, the number of points in the point set with POS as the parent node)NODE[FA].C+=NODE[POS].C;//Update the point set, the weight will naturally be updated .Node[fa].w= (Double) node[fa].c/node[fa].t;//Greedy principle: The number of new point centralization and/or new point concentration points} printf ("%d\n", RES); }    return 0;}

POJ 2054 Color A tree# greedy (difficult, good question)

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.