-
Notifications
You must be signed in to change notification settings - Fork 69
/
#167_Hamiltonian_Flights.cpp
47 lines (41 loc) Β· 1.04 KB
/
#167_Hamiltonian_Flights.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxN = 20;
const int SIZE = 1<<maxN;
const ll MOD = 1e9+7;
int N, M;
bool inqueue[maxN][SIZE];
ll dp[maxN][SIZE];
vector<int> G[maxN];
queue<pii> Q;
int main(){
scanf("%d %d", &N, &M);
for(int i = 0, a, b; i < M; i++){
scanf("%d %d", &a, &b);
G[a-1].push_back(b-1);
}
dp[0][1] = 1;
Q.push({0, 1});
inqueue[0][1] = true;
while(!Q.empty()){
int u = Q.front().first;
int mask = Q.front().second;
Q.pop();
if(u != N-1){
for(int v : G[u]){
int newMask = mask|(1<<v);
if((mask&(1<<v)) == 0){
dp[v][newMask] += dp[u][mask];
dp[v][newMask] %= MOD;
if(!inqueue[v][newMask]){
Q.push({v, newMask});
inqueue[v][newMask] = true;
}
}
}
}
}
printf("%lld\n", dp[N-1][(1<<N)-1]);
}