DP On Graphs
DP On Graphs
DP on DAGs
- Only on DAG (Directed Acyclic Graph)
[ Directed graph without a cycle ]
If( k<=p)
{
ans = Kth successor of p
}
Else
{
k-=p
ans = (k%L)th successor of 1
(1 is the node where cycle starts)
}
Binary Lifting
a = succ(x)
b = succ(succ(x))
while (a!=b)
{
a=succ(a)
b=succ(succ(b))
}
Distance of a = k + p.L + m
Distance of b = k + q.L + m
Distance of b = 2 x Distance of a
k + q.L + m = 2 (k + p.L + m)
(q-2p).L = k + m
Let (q-2p) = z
k+m = L.z
k+m is a multiple of L
K = L.z -m
b=x
while (a!=b)
{
a=succ(a)
b=succ(b)
}
// Now, after this value of a would be C
(entry point)