-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_routes.cpp
More file actions
57 lines (51 loc) · 898 Bytes
/
Copy pathgame_routes.cpp
File metadata and controls
57 lines (51 loc) · 898 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
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int mod = 1000000007;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> graph(n);
vector<int> indegree(n, 0);
vector<long long> ways(n, 0);
for(int i = 0; i < m; ++i) {
int from, to;
cin >> from >> to;
graph[--from].push_back(--to);
indegree[to]++;
}
queue<int> q;
for(int i = 1; i < n; ++i) {
if(indegree[i] == 0) {
q.push(i);
}
}
while(!q.empty()) {
int u = q.front();
q.pop();
for(int v : graph[u]) {
indegree[v]--;
if(indegree[v] == 0 && v != 0) {
q.push(v);
}
}
}
queue<int> q1;
q1.push(0);
ways[0] = 1;
while(!q1.empty()) {
int curr = q1.front();
q1.pop();
for(int to : graph[curr]) {
ways[to] += ways[curr];
ways[to] %= mod;
indegree[to]--;
if(indegree[to] == 0) {
q1.push(to);
}
}
}
cout << ways[n-1] << endl;
return 0;
}