-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopological_sort_binary.cpp
More file actions
58 lines (50 loc) · 1.06 KB
/
Copy pathtopological_sort_binary.cpp
File metadata and controls
58 lines (50 loc) · 1.06 KB
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
58
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<utility>
#include<memory>
#include<cmath>
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define pb push_back
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int INF=1<<30;
map<int,int> mp;
int N;
ll memo[1<<17];
ll calc(int S){ //トポロジカルソートの通り数をbitを頂点グラフとしたメモ化再帰で求める
if(memo[S]!=-1) return memo[S];
memo[S]=0;
rep(i,N){
int comb=1<<i;
if(!(comb&S)) continue;
if((S&(~comb))&mp[comb]) continue; //辺がある場合
memo[S]+=calc(S&(~comb));
}
return memo[S];
}
int main(){
int M;
cin >> N >> M ;
fill(memo,memo+(1<<16),-1);
int x,y;
rep(i,M){
cin >> x >> y ;
x = 1 << (x-1);
y = 1 << (y-1);
if(EXIST(mp,x)) mp[x]=(mp[x]|y);
else mp[x]=y;
}
memo[0]=1;
cout << calc((1<<N)-1) << endl;
return 0;
}