We consider dynamic programming. Let $$$\mathrm{dp}_{i,j,0/1}$$$ represent the expected number of steps when currently at node $$$i$$$ with $$$j$$$ coins and about to take an even/odd step. To simplify the formulation: - Define $$$\mathrm{sc}_i := \text{number of children of } i, p_i := \text{parent of } i$$$. - The initial condition is $$$\mathrm{dp}_{1,j,0/1} = 1$$$.
Odd steps are straightforward:
$$$ \mathrm{dp}_{i,j,1} = \mathrm{dp}_{p_i,j,0} + 1. $$$From the problem description:
$$$ \mathrm{dp}_{i,j,0} = 1 + \begin{cases} \mathrm{dp}_{p_i,j-1,1}, & \text{(to pay)} \\ \dfrac{\mathrm{dp}_{p_i,j,1} + \sum_{u \text{ is a child of } i} \mathrm{dp}_{u,j,1}}{\mathrm{sc}_i + 1}, & \text{(not to pay)} \end{cases}. $$$The "not to pay" case involves both the parent and children, making it more complex. However, observe that:
$$$ \forall u \text{ is a child of } i : \mathrm{dp}_{u,j,1} = \mathrm{dp}_{p_u,j,0} + 1 = \mathrm{dp}_{i,j,0} + 1. $$$This implies that for the "not to pay" case:
$$$ \begin{aligned} \mathrm{dp}_{i,j,0} &= 1 + \dfrac{\mathrm{dp}_{p_i,j,1} + \sum (\mathrm{dp}_{i,j,0} + 1)}{\mathrm{sc}_i + 1} \\ &= \dfrac{\mathrm{dp}_{p_i,j,1} + \mathrm{sc}_i \mathrm{dp}_{i,j,0} + 2 \mathrm{sc}_i + 1}{\mathrm{sc}_i + 1}. \end{aligned} $$$Reorganizing:
$$$ (\mathrm{sc}_i + 1) \mathrm{dp}_{i,j,0} = \mathrm{dp}_{p_i,j,1} + \mathrm{sc}_i \mathrm{dp}_{i,j,0} + 2 \mathrm{sc}_i + 1, $$$ $$$ \mathrm{dp}_{i,j,0} = \mathrm{dp}_{p_i,j,1} + 2 \mathrm{sc}_i + 1. $$$Thus:
$$$ \mathrm{dp}_{i,j,0} = \min \{\mathrm{dp}_{p_i,j-1,1}, \mathrm{dp}_{p_i,j,1} + 2 \mathrm{sc}_i\} + 1. $$$
Or, what's the more basic logic behind this
I have misread the constraints that $$$q = 3e5$$$ and did the same in the contest ToT 295629246
when i'm vping this i just couldn't understand why isn't $$$q\le 2\times 10^6$$$ lol
Please formulate a mathematically sound and logical query with everything correctly defined, I cannot understand what you actually tried to ask
However, what I suspect you want is the following: suppose we are in a state, now we must decide to pay coin or not. If we pay coin, then we leave and all is good. Otherwise, we could come back to the same place after one random step. Now it seems that we have to decide to pay or not yet again, but this is not necessary as the process is Markov/Stateless/Memoryless, nothing actually changed between the first time or later time you arrived (the total sunk cost did change, but you are only asking for expected cost, so it does not affect the expected cost. Note however if you are asking for probability of total cost <=k then it will change). This means you either always pay or never pay at this exact state, giving why the DP is valid.
The rearrangement could seem surprising in a dp but that is just a general equation solving in a random (here, markov chain) process.
thank you for introducing the Markov chain (✿◠‿◠)
upd:
For all $$$h(x,y)$$$, this doesn't always hold. For example:
then we have $$$x_0=1,x_1=114514,h(x_0,x_1)=2,h(f(2),g(2))=8$$$, so $$$h(x_0,x_1)$$$ is not a fixed point of $$$h$$$.
however, if we have $$$\forall x,y:h(x,y)=x\lor h(x,y)=y$$$, like $$$h(x,y)=\min(x,y)$$$, we can say that $$$x_0,x_1$$$ are the only possible fixed points.
let's define $$$A:=\{x\mid h(f(x),g(x))=f(x)\},B:=\{x\mid h(f(x),g(x))=g(x)\}$$$, then if $$$x\in A$$$, the fixed point of $$$h(f(x),g(x))=f(x)$$$ should be $$$x_0$$$. else if $$$x\in B$$$, it should be $$$x_1$$$. now, $$$x_0,x_1$$$ are the only possible fixed points for $$$h$$$. what we need to do is to check if $$$h(x_0,g(x_0))=x_0$$$ and $$$h(f(x_1),x_1)=x_1$$$.
thanks to my classmate MasCotangent for math helping :)