在续挑战这本书,看了下状压但是不是很懂,因为老感觉他那几重循环有好多地方是有问题的,但是也不太清楚.
这一题是练习题第一题,然后我想了两个小时才出来,然后发现时间是人家十倍,空间是两倍,就很难过了.
大概我的思维是独一份的把,独一份的蠢..
dp[s][time] 在上一次time(滚动数组)之后,分配方式为s的方法有多少种.
/* xzppp */
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson MAXN,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
typedef long long ll;
typedef unsigned long long ULL;
const int MAXN = 20;
const int MAXM = 20;
const int INF = 0x7fffffff;
const int MOD = 1e9+7;
int dp[1<<MAXN][2];
vector<int > G[MAXN+17];
int main(int argc, char const *argv[])
{
//FFF
int n,m;
scanf("%d%d",&n,&m);
for (int i = 0; i < n; ++i)
{
int p;
scanf("%d",&p);
for (int j = 0; j < p; ++j)
{
int temp;
scanf("%d",&temp);
temp--;
G[i].PB(temp);
}
}
int ans = 0;
for (int i = 0; i < G[0].size(); ++i)
dp[1<<G[0][i]][0] = 1;
int now = 0,last = 1;
for (int i = 1; i < n; ++i)
{
swap(now,last);
for (int s = (1<<m)-1; s > -1; --s) dp[s][now] = 0;
for (int j = 0; j < G[i].size(); ++j)
for (int s = (1<<m)-1; s > -1; --s)
if(!((s>>G[i][j])&1)&&dp[s][last]>0)
{
dp[s|(1<<G[i][j])][now] += dp[s][last];
if(i==n-1)
ans+=dp[s][last];
}
}
printf("%d\n",ans);
return 0;
}