File tree 1 file changed +68
-0
lines changed
1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ #define int long long
4
+ #define mod (int ) (1e9 +7 );
5
+ const int N=1e5 +10 ;
6
+ const int INF=1e18 ;
7
+ int n,m;
8
+ vector<int >adj[N];
9
+ bool detectCycle (vector<int >&vis,int v){
10
+ if (vis[v]==2 )return true ;
11
+ vis[v]=2 ;
12
+ for (auto it:adj[v]){
13
+ if (vis[it]!=1 ){
14
+ if (detectCycle (vis,it)){
15
+ return true ;
16
+ }
17
+ }
18
+ }
19
+ vis[v]=1 ;
20
+ return false ;
21
+ }
22
+ bool cycleDect (int n){
23
+ vector<int > vis (n+1 ,0 );
24
+ for (int i=0 ;i<n;i++){
25
+ if (!vis[i]){
26
+ if (detectCycle (vis, i)){
27
+ return true ;
28
+ }
29
+ }
30
+ }
31
+ return false ;
32
+ }
33
+ void topoDfs (int v,vector<int >& visited,vector<int >& res){
34
+ visited[v]=1 ;
35
+ for (auto it:adj[v]){
36
+ if (!visited[it]){
37
+ topoDfs (it,visited, res);
38
+ }
39
+ }
40
+ res.push_back (v);
41
+ }
42
+ signed main () {
43
+ cin>>n>>m;
44
+ vector<pair<int ,int >>g[n+1 ];
45
+ for (int i=0 ;i<m;i++){
46
+ int x,y,wt;
47
+ cin>>x>>y;
48
+ adj[x].push_back (y);
49
+ }
50
+ vector<int > visited (n+5 ,0 );
51
+ vector<int >res;
52
+ for (int i=1 ;i<=n;i++){
53
+ if (!visited[i]){
54
+ topoDfs (i,visited,res);
55
+ }
56
+ }
57
+ reverse (res.begin (),res.end ());
58
+ vector<int >dp (n+1 );
59
+ dp[1 ]=1 ;
60
+ for (auto it:res){
61
+ for (auto x:adj[it]){
62
+ dp[x]=(dp[x]+dp[it])%mod;
63
+ }
64
+ }
65
+ cout<<dp[n]<<endl;
66
+ return 0 ;
67
+ }
68
+
You can’t perform that action at this time.
0 commit comments