HDU 5005 compromise (2014 acmicpc regional Anshan site 1009)

Source: Internet
Author: User


Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5005


The question is too long, so I don't post the question, so I will directly talk about the general idea (the general idea is not short ....).

There are two people, a and X, and a directed acyclic graph (DAG). Each node with an outbound degree of 0 (these nodes are called "leaf nodes" below ") there are two values X and Y (all X and Y are different, which is very important ). All nodes except the leaf node are controlled by a or X. If the current position is in a non-leaf node u, if u is controlled by a, a can select the next point V (if u-> V has a directed edge), and vice versa. If a leaf node is reached, a gets x points and X gets y points. Now a and X play a game. Starting from 1 node, everyone wants to score the most (important ). Now X has a special operation. He can tell a his policy that the policy is the transfer node selected by X at the point controlled by X. (X will certainly follow the rule, at least a thinks so ). Now I have two questions:

1. x maximum score obtained when no special operation is used.

2. x indicates the maximum score that can be obtained when a special operation is used.


Idea: the first question is extremely simple, because it is a dag, set DP [I] [0], DP [I] [1] indicates the maximum score obtained by node A and node X. Then, the transfer can be performed in three cases: leaf node, A-controlled node, and X-control node.

The key is the second question. At first glance, it seems that it is silly to tell the other party the strategy, and it does not improve the final score, however, considering that both parties have the goal of maximizing their own score, rather than minimizing the score of the other party or Maximizing the difference between themselves and the other party (this is important ). Therefore, X can adopt a strategy to make its score greater than that without special operations. For more information, see the example. As for how to develop your own policies, it seems difficult to directly formulate them (I won't do it). We can change the way of thinking, because the final score of A and X must be the weight of a leaf node, then, we can enumerate the values of Y from large to small to determine whether the leaf node can be reached by a certain policy of B, if it reaches the clear answer, it is the y weight of the leaf node to be enumerated (because it is from large to small enumeration ).

Now consider a leaf node, set to v. Whether V can be reached depends not only on X, but also on a's mood. x certainly wants to reach V, because V is the best choice so far, none of the above can be reached (otherwise, V will not be enumerated ...), Now the question is whether a will also reach v. Set the x weight of V to limit. If a can reach a better leaf node so that its weight is greater than limit, then a will not select v, now, the key issue is to ensure that a cannot reach a leaf node that is better than V (For. Let's define a node as "good". If and only starting from this node, no matter how a is transferred, X can use a policy to make a unable to reach a leaf node better than v. Otherwise, this node is called "bad ". Then we can calculate whether each node is "good" or "bad" based on DP ".

For a leaf node, if its X value is greater than limit, it is "bad"; otherwise, it is "good ".

For non-leaf nodes, (1) if it is controlled by a and its successor node is bad, it means that a can reach a node better than v, it means it is "bad". Otherwise, a can only reach the "good" point in any case, and it is "good ".

(2) If it is controlled by X and one of its successor nodes is "good", it means that X can be transferred to a "good" node, so it is "good", otherwise it is "bad ".

If node 1 is "bad", it means that a can select a leaf node that is better than node v and cannot reach node v. If node 1 is a "good" node, it only means that X can use a policy to make a unable to reach a leaf node that is better than v.

V can be reached based on: if there is a simple path composed of only "good" points from node 1 to V, it means that V can be reached.

At this time, you only need to judge it from node 1 DFS once.

Now, I want to explain why it reaches v. Because Node 1 is "good", it means that for a, V is the best leaf node for him (other better nodes have been marked as "bad ", X won't let a go to the "bad" node.) At the same time, V is also the optimal node for X (as mentioned earlier ), now there is another path from node 1 to node V which is composed of only good nodes. Then V is reachable. This question is finished.

Finally, let's talk about X's policy. From the above analysis, we can see that if V is reachable, X just tells a: "For all my control points, I will inevitably move to a good successor node (which depends on V's choice ). In this case, node A can only select node v as the optimal node. I finally finished writing .....

The Code is as follows: (the version is not optimized, so it is too long to write ...)

# Include <iostream> # include <string. h> # include <stdio. h >#include <algorithm> # include <vector> # define maxn 210 using namespace STD; struct edge {int to, next;} e [maxn]; int box [maxn], CNT; void Init () {memset (box,-1, sizeof (box); CNT = 0;} void add (int from, int to) {e [CNT]. to = to; E [CNT]. next = Box [from]; Box [from] = CNT ++;} int bel [maxn], a [maxn], B [maxn]; struct node {int ID, a, B; node () {} node (int x, int y, int Z) {I D = X, A = Y, B = z ;}}; bool CMP (const node & X, const node & Y) {return X. b> Y. b;} vector <node> VEC; int DP [maxn] [2], vis [maxn]; void dfs1 (INT now) {If (DP [now] [0]! =-1) return; If (bel [now] =-1) // does not belong to anyone {DP [now] [0] = A [now]; DP [now] [1] = B [now]; return;} For (INT T = Box [now]; t + 1; t = E [T]. next) {int v = E [T]. to; dfs1 (V); If (bel [now] = 0) // belongs to a {If (DP [v] [0]> DP [now] [0]) {DP [now] [0] = DP [v] [0]; DP [now] [1] = DP [v] [1] ;}} else // belongs to X {If (DP [v] [1]> DP [now] [1]) {DP [now] [0] = DP [v] [0]; DP [now] [1] = DP [v] [1] ;}} int DFS (INT now, int limit) {If (vis [now]! =-1) return vis [now]; If (bel [now] =-1) {if (a [now]> limit) return vis [now] = 0; return vis [now] = 1;} If (bel [now] = 0) {for (int t = Box [now]; t + 1; T = E [T]. next) {int v = E [T]. to; DFS (v, limit);} For (int t = Box [now]; t + 1; t = E [T]. next) {int v = E [T]. to; If (! Vis [v]) return vis [now] = 0;} return vis [now] = 1;} else {for (int t = Box [now]; t + 1; T = E [T]. next) {int v = E [T]. to; DFS (v, limit);} For (int t = Box [now]; t + 1; t = E [T]. next) {int v = E [T]. to; If (vis [v]) {return vis [now] = 1 ;}} return vis [now] = 0 ;}} bool dfs2 (INT now, int limit) {If (bel [now] =-1) {if (a [now] = Limit) return true; return false ;}for (int t = Box [now]; t + 1; t = E [T]. next) {int v = E [T]. to; If (vis [v] = 1) {If (dfs2 (v, limit) return true ;}} return false ;} int main () {// freopen ("dd.txt", "r", stdin); int ncase; scanf ("% d", & ncase); While (ncase --) {int N; scanf ("% d", & N); Init (); Vec. clear (); memset (BEL,-1, sizeof (BEL); memset (DP,-1, sizeof (DP); For (INT I = 1; I <= N; I ++) {int num; scanf ("% d", & num); If (num = 0) {scanf ("% d", & A [I], & B [I]); Vec. push_back (node (I, a [I], B [I]);} else {int X; For (Int J = 0; j <num; j ++) {scanf ("% d", & X); add (I, x) ;}char typ [10]; scanf ("% s", typ ); if (typ [0] = 'A') bel [I] = 0; else bel [I] = 1 ;}} dfs1 (1 ); int ans1 = DP [1] [1]; printf ("% d", ans1); sort (VEC. begin (), VEC. end (), CMP); int ans2 =-1; for (INT I = 0; I <(INT) Vec. size (); I ++) {memset (VIS,-1, sizeof (VIS); int now = VEC [I]. ID; If (DP [now] [0] =-1) continue; If (DFS (1, VEC [I]. a) {If (dfs2 (1, VEC [I]. a) {ans2 = VEC [I]. b; break ;}}printf ("% d \ n", ans2) ;}return 0 ;}



HDU 5005 compromise (2014 acmicpc regional Anshan site 1009)

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.