### LovesProgramming's blog

By LovesProgramming, history, 10 months ago, , This is a graph problem from the previous contest by Codenation .

Can you suggest some ideas to approach it or sketch the solution ?

Problem :- You are given a DAG(Directed A-cyclic Graph)

You are also given a source node- 'X' as input .

For each node 'Y' , find the number of paths that start at 'X' and end at 'Y' , such that the number of nodes visited along that path is even(including X an Y)

Note:- The path from X to X consists of only one node and hence the number of the nodes visited is odd. Hence there are 0 paths for X to X which consist of even number of nodes.

The number of test-cases:- 20

Number of nodes in the graph(n):- 100000.

Edges:- [1,minimum(100000,n*(n-1)/2]

Output:-'n' space separated integers such that the i'th element is the answer for node 'i', mod 1000000007

Thanks :-)  Comments (6)
 » Auto comment: topic has been updated by LovesProgramming (previous revision, new revision, compare).
 » We can think of it as this way : We can reach a node by any of it's parents. Here, by parent, I mean any such node that has an outgoing edge to this node. So, the number of ways of reaching a node is the sum of the number of ways of reaching it's parents. Now, we know from observation 2 that we need to find out the number of ways of reaching all the parents of a node, to find out the number of ways to reach that node. From observation 3, we know that we need to process nodes in topological order. (think on it, if you didn't get it because this is the most important observation). After this, it's simple implementation leftImplementation : Make topological sorting of nodes. Make a 2D DP — DP[N]. Here DP[i] stores the number of paths from node X to node i having even number of nodes and DP[i] stores the number of paths from node X to node i having odd number of nodes. One can easily see that DP[X] = 1, as there is one path having odd number of nodes from X to X. Keep all other values of DP table as 0. Now let suppose that topsort is a vector of nodes in topological order. And parent[i] is a vector of nodes containing parent of node i. You have this vector for each i such that 1 <= i <= n. If for a path, the number of nodes from X to a parent is even, then the number of nodes from X to that parent's child will be odd, and vice versa. This leads us to the following transition. for(auto i : topsort){ for(auto j : parent[i]){ dp[i] = (dp[i] + dp[j]) % MOD; dp[i] = (dp[i] + dp[j]) % MOD; } } Now, the answer for each node i is in dp[i].I hope it helps :)If you have any doubts / need further clarifications, then feel free to ask.
•  » » » How would you account for multiple paths from source node $x$ to destination nodes $i$? If you allowed revisiting the nodes in the $DFS$ to account for the multiple paths, this would lead to an in-efficient solution. Topological ordering accounts for multiple paths to nodes in $DAG$s without revisiting nodes, by using the observation that no two nodes are reachable from one another. So, once answers of all nodes $p$ which have outgoing edges to node $i$ are computed, answer of node $i$ is ready to be computed.