Introduction to algorithms-15-4-planning a company gathering

Source: Internet
Author: User

Question:

Thoughts:

Each node in the tree represents an employee and three domains are added to each node P. enthu [p] indicates the employee's favorite value represented by node p. Meaning of in [p]: If P participates in a party, the tree with P as the root can get the largest favorite value. Meaning of notin [p]: If P does not attend the party, the tree with P as the root can get the largest favorite value.

Initialization: If P is a leaf node, in [p] = enthu [p], notin [p] = 0

Subproblem: In [p] = enthu [p] + sum (notin [p-> child]), notin [p] = sum (max (in [p-> child], notin [p-> child])

The condition for calculating the current node is that the child node has already calculated the result. To facilitate programming, the program uses an adjacent graph to represent the management relationship tree.

Step 1: Construct the management relationship tree g (representing the edge from the parent to the child using the adjacent graph)

Step 2: Perform the transpose operation G2 on the management relationship tree (Introduction to algorithms-22.1-3-transpose of Directed Graphs), that is, the edge from the child to the parent

Step 3: Calculate the topological sorting of G2 and perform DP according to this sorting (Introduction to algorithms-22.4-5-use queues for topological sorting)

Step 4: Calculate the in [p] and notin [p] of each vertex in sequence according to the sequence of step 3.

Step 5: Manage the root node root of the relational tree, that is, the last vertex in the topological sequence. Max (in [root], notin [root]) is the value.

Code:

# Include <iostream> # include <queue> using namespace STD; # define N 5 // Number of vertices # define M 4 // Number of edges int n, m; queue <int> q; // used for topological sorting // edge node Structure struct edge {int start; // the start point int end of the directed graph; // The end edge of the directed graph * next; // edge (int s, int e) pointing to the next edge of the same starting point: start (s), end (E), next (null ){}}; // vertex node Structure struct vertex {int indegree; // inbound, used for topological sorting int in; // If P participates in the gathering, the maximum dislike value int notin that can be obtained from the tree with P as the root; // If P does not attend the party, the maximum dislike value int enthu can be obtained from the tree with P as the root; // node P indicates the employee's preferred value edge * head ;/ /Point to the next vertex (): indegree (0), head (null ){}}; // graph structure struct graph {vertex * V [10 + 1]; // n vertex graph () {int I; for (I = 1; I <= N; I ++) V [I] = new vertex ;}; // insert an edge void insertedge (graph * g, edge * E) {// if no edge with the same starting point exists (G-> V [E-> Start]-> head = NULL) g-> V [E-> Start]-> head = E; // If yes, add else {e-> next = G-> V [E-> Start]-> head to the linked list; g-> V [E-> Start]-> head = E;} G-> V [E-> end]-> indegree ++ ;} // transpose graph * reverse (graph * g) {graph * ret = ne W graph; int I; // traverse each edge in graph G, starting from the end point, ending from the start point, and adding it to the ret of The New Graph for (I = 1; I <= N; I ++) {edge * E = G-> V [I]-> head; while (E) {edge * E = new edge (e-> end, e-> Start); insertedge (Ret, e); E = e-> next ;}} return ret ;} // calculate the in [p] and notin [p] void sub (graph * g, vertex * P) of each vertex according to the topological sequence of the transpose graph) {// If P is a leaf node, in [p] = enthu [p], notin [p] = 0 p-> In = p-> enthu; p-> notin = 0; edge * E = p-> head; // If P has children while (E) {// in [p] = enthu [p] + sum (notin [p-> child]) P-> In = p-> in + G-> V [E-> end]-> notin; // notin [p] = sum (max (in [p-> child], notin [p-> child]) p-> notin = p-> notin + max (G-> V [E-> end]-> notin, g-> V [E-> end]-> In); E = e-> next;} void dp (graph * G1, graph * G2) {// sort the topology of the transpose graph while (! Q. empty () Q. pop (); int I, RET; for (I = 1; I <= N; I ++) if (G2-> V [I]-> indegree = 0) q. push (I); While (! Q. empty () {int q = Q. front (); q. pop (); // calculate the in [p] and notin [p] sub (G1, G1-> V [Q]) of each vertex in sequence based on the topological sequence of the transpose graph. ret = max (G1-> V [Q]-> In, G1-> V [Q]-> notin ); // edge * E = G2-> V [Q]-> head; while (e) {G2-> V [E-> end]-> indegree --; if (G2-> V [E-> end]-> indegree = 0) Q. push (e-> end); E = e-> next ;}// root node of the management relationship tree, that is, the last vertex in the topological sequence, max (in [root], notin [root]) is the cout <RET <Endl ;} /* 6 2 5 1 11 21 33 43 5 */INT main () {While (CIN> N> m) {// construct an Empty Graph graph * g = new graph; int I, start, end; // enter everyone's liking for (I = 1; I <= N; I ++) CIN> G-> V [I]-> enthu; // input edge for (I = 1; I <= m; I ++) {CIN> Start> end; edge * E = new edge (START, end); insertedge (G, e );} // transpose graph * g2 = reverse (g); DP (G, G2);} return 0 ;}

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.