题目描述:
题解:
难道是上一道的状压dp给了我自信?难道还是我真的有做dp的天赋?(逃
这里我们直接枚举(1<<17)种情况就好了。如果当前状态满足。
那我们就在这个状态的基础上减掉一种算法,然后检测这种状态是否满足。如果满足,我们就可以直接转移了。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long LL;
int n,m;
int f[20][20];
LL dp[(1<<18)];
bool ok(int j)
{
for(int i=0;i<n;i++)
{
if((j>>i)&1)
{
for(int k=0;k<n;k++)
{
if(f[i][k])
{
if(!((j>>k)&1))return 0;
}
}
}
}
return 1;
}
void print(int x)
{
while(x)
{
cout<<(x&1);
x>>=1;
}
cout<<endl;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
f[b-1][a-1]=1;
}
dp[0]=1;
for(int i=1;i<(1<<n);i++)
{
if(ok(i))
{
for(int j=0;j<n;j++)
{
if((i>>j)&1)
{
int x=i-(1<<j);
if(ok(x))
{
//cout<<i<<" "<<x<<endl;
dp[i]+=dp[x];
}
}
}
}
}
printf("%lld",dp[(1<<n)-1]);
}