1#include <bits/stdc++.h>2usingnamespace std;3constint mod =1e9+7;4//idea is to use toposort to ensure that u always before v for every direct edge u - v and then accumulate cnt of paths 5intmain(){6 ios::sync_with_stdio(0);7 cin.tie(0);8int n, m; cin >> n >> m;9vector<vector<int>>adj(n +1);10vector<int>indegree(n +1);11for(int i =0; i < m; i++){12int a, b; cin >> a >> b;13 adj[a].push_back(b);14 indegree[b]++;15 }16queue<int> q;17vector<int> res;18for(int i =0; i < indegree.size(); i++){19if(indegree[i] ==0) q.push(i);20 }21while(!q.empty()){22int node = q.front(); q.pop();23 res.push_back(node);24for(int nxt : adj[node]){25if(!--indegree[nxt]) q.push(nxt);26 }27 }28vector<int>dp(n +1); dp[1] =1; // base case29for(int i =0; i < res.size(); i++){30int cur = res[i];31for(int nxt : adj[cur]){32 dp[nxt] += dp[cur];33 dp[nxt] %= mod;34 }35 }36 cout << dp[n] <<'\n';37return0;38}
☕
Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.
Python records executed lines and locals automatically. For selected values in any language, add // @trace i, total on its own valid line; Python uses # @trace i, total.
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.
Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.