Count Ways

You have been given a directed graph of 'V' vertices and 'E' edges.

Your task is to count the total number of ways to reach different nodes in the graph from the given source node ‘S’. A way is called as different from others if the destination node or used edges differ.

As the total number of such ways can be large, print the total ways modulo 10 ^ 9 + 7.

Note:

1. Include the source node as a destination also.
2. There are no cycles in the given graph.
Input Format:
The first line of input will contain three integers 'V', 'E', and ‘S’, separated by a single space.

From the second line onwards, the next 'E' lines will denote the directed edge of the graphs.

Every edge is defined by two single space-separated integers 'A' and 'B', which signifies a directed edge from vertex 'A' to vertex 'B'.
Output Format:
For each input, print a single line containing a single integer denoting the total number of ways modulo 10 ^ 9 + 7.
Constraints:
1 <= 'S', 'V' <= 10 ^ 5
0 <= 'E' <= 2 * 10 ^ 5

Where ‘S’ represents the value of a given source node, ‘V’ represents the number of vertices and ‘E’ represents the number of edges.

Time Limit: 1 sec.
CodingNinjas
author
2y
Brute Force

In the brute force approach, we will visit all paths from the source node to each node and increment the ‘COUNT’ for each path. We will use a recursive function that will be having a parame...read more

CodingNinjas
author
2y
Memoization Approach

In the approach, there exist repeated subproblems. To avoid calculating them every time, we will use the memoization concept to memorize the results once calculated. So that whenev...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter