-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameRoutes.cpp
More file actions
46 lines (43 loc) · 903 Bytes
/
GameRoutes.cpp
File metadata and controls
46 lines (43 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n;
vector<int> edge[100001];
vector<int> backedge[100001];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int m; cin >> n >> m;
int in_degree[n+1], dp[n+1];
for(int i = 0; i <= n; i++){
in_degree[i] = 0;
dp[i] = 0;
}
dp[1] = 1;
for(int i = 0; i < m; i++){
int a,b; cin >> a >> b;
edge[a].push_back(b);
backedge[b].push_back(a);
in_degree[b]++;
}
//uses Kahn's algorithm
queue<int> q;
for(int i = 0; i < n; i++) {
if(in_degree[i] == 0) {
q.push(i);
}
}
while(!q.empty()) {
int node = q.front();
q.pop();
for(int next : edge[node]) {
in_degree[next]--;
if(in_degree[next] == 0) q.push(next);
}
for(int prev : backedge[node]) {
dp[node] = (dp[node] + dp[prev]) % 1000000007;
}
}
cout << dp[n] << endl;
return 0;
}