Hdu_3660
First, if the path cannot be set to within L or R after the next step reaches a node, then this step cannot be taken. As for how to find these nodes that cannot be taken, you can start from the leaf node. If the path is not within L or R, mark the leaf as-1, it indicates that the node cannot come. For other nodes, if all their children are marked as-1, it should also be marked as-1.
In this way, we only need to go along the non-1 point. How can we calculate the maximum possible value next? For Bob, it is clear that he should select the path with the "maximum possible value" in all paths, alice should choose the path "the smallest possible value in the end", so how can she know what the final value is? You only need to start from the leaf node and calculate it in sequence.
In addition, if this question does not need to be read and optimized, it is easier to time out.
# Include <stdio. h> # Include < String . H> # Include <Algorithm># Define Maxd 500010 # Define INF 0x3f3f3f Int N, l, R, first [maxd], E, next [maxd], V [maxd], W [maxd], Col [maxd], DP [maxd], dis [maxd]; Int Q [maxd]; Void Add ( Int X, Int Y, Int Z) {v [E] = Y, W [e] = Z; next [E] = First [X], first [x] = e ++;} Void Scan ( Int & A ){ Char C; While (C = getchar () = ' ' | C = ' \ N ' ); = C- 48 ; While (C = getchar ()> = ' 0 ' & C <= ' 9 ' ) = (A < 3 ) + (A < 1 ) + C- 48 ;} Void Init (){ Int I, x, y, z; memset (first, - 1 , Sizeof (First [ 0 ]) * N), E = 0 ; For (I = 1 ; I <n; I ++ ) Scan (x), scan (Y), scan (z), add (x, y, z );} Void Solve (){ Int I, j, X, rear = 0 , Flag; Col [ 0 ] = 1 , DIS [ 0 ] = 0 , Q [rear ++] = 0 ; For (I = 0 ; I <rear; I ++ ) {X = Q [I]; For (J = first [X]; J! =- 1 ; J =Next [J]) COL [V [J] = Col [x] ^ 1 , DIS [V [J] = dis [x] + W [J], Q [rear ++] = V [J];} For (I = rear- 1 ; I> = 0 ; I -- ) {X = Q [I]; flag = 0 ; If (COL [x] = 0 ) {DP [x] = INF; For (J = first [X]; J! =- 1 ; J = Next [J]) {flag = 1 ; If (DP [V [J]! =- 1 ) DP [x] = STD: min (DP [X], DP [V [J] + W [J]) ;}} Else {DP [x] =-INF; For (J = first [X]; J! =- 1 ; J = Next [J]) {flag = 1 ; If (DP [V [J]! =- 1 ) DP [x] = STD: max (DP [X], DP [V [J] + W [J]) ;}} If (Flag = 0 ) DP [x] = dis [x]> = L & dis [x] <= r? 0 :-1 ; Else If (DP [x] = inf | DP [x] =-INF) DP [x] =- 1 ;} If (DP [ 0 ]> = L & DP [ 0 ] <= R) printf ( " % D \ n " , DP [ 0 ]); Else Printf ( " Oh, my God! \ N " );} Int Main (){ While (Scanf ( " % D " , & N, & L, & R) = 3 ) {Init (); solve ();} Return 0 ;}