You are not logged in. Please login at www.codechef.com to post your questions!

×

# NUMPATH - Editorial

Author: Vineet Paliwal
Tester: Roman Rubanenko
Editorialist: Jingbo Shang

Medium

# PREREQUISITES:

Dynamic Programming, Suffix Sum, Fenwick Tree

# PROBLEM:

Given a Directed-Acyclic-Graph (DAG) G = (V, E) in which node i has edges to nodes in [i + 1, i + N[i]], find how many paths are there between S[i] and T.

# EXPLANATION:

This DAG is really special and the order of 1 ... V is exactly same as its topo order in which edges are only existed from previous nodes to their later ones.

Use F[i] to state the number of different paths starting from node i to node T.

    Initially F[T] = 1, F[others] = 0.


The transmission can be described as following:

    For i = T - 1 downto 1
F[i] = \sum_{v = i + 1} ^ {i + N[i]}


To speed up this transmission procedure, we can use a Fenwick Tree to get the sum. But we can achieve it in a simpler way as following, using suffix sum.

    suffixSum[] = 0;
suffixSum[T] = 1;
For i = T - 1 downto 1
F[i] = G[i + N[i]];
G[i] = G[i + 1] + F[i]


To answer each query, just directly output the F[S[i]]. Therefore, the time complexity is O(N + Q) in total.

# AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution can be found here.
Tester's solution can be found here.

This question is marked "community wiki".

asked 18 Nov '13, 00:21 19.8k350498541
accept rate: 36% 161446376

 0 http://www.codechef.com/viewsolution/7049475 Can anyone suggest how to reduce time? I am exceeding the time limit. I am working with C. answered 30 May '15, 20:16 0★xadon 1●1 accept rate: 0%
 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,852
×2,658
×2,214
×146
×11

question asked: 18 Nov '13, 00:21

question was seen: 3,337 times

last updated: 30 May '15, 20:19