# POJ 2441 状态压缩+动态规划（好题）

## Description

Farmer Johnson’s Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because they believe that the others are all very weak. Farmer Johnson has N cows (we number the cows from 1 to N) and M barns (we number the barns from 1 to M), which is his bulls’ basketball fields. However, his bulls are all very captious, they only like to play in some specific barns, and don’t want to share a barn with the others.

So it is difficult for Farmer Johnson to arrange his bulls, he wants you to help him. Of course, find one solution is easy, but your task is to find how many solutions there are.

You should know that a solution is a situation that every bull can play basketball in a barn he likes and no two bulls share a barn.

To make the problem a little easy, it is assumed that the number of solutions will not exceed 10000000.

## Input

In the first line of input contains two integers N and M (1 <= N <= 20, 1 <= M <= 20). Then come N lines. The i-th line first contains an integer P (1 <= P <= M) referring to the number of barns cow i likes to play in. Then follow P integers, which give the number of there P barns.

## Output

Print a single integer in a line, which is the number of solutions.

3 4
2 1 4
2 1 3
2 2 4

4

## 代码（TLE）

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<utility>
#include<cmath>
#include<limits.h>
#include<math.h>
#include<map>
#include<climits>
#include<numeric>
#include<ctype.h>
#include<fstream>
#define inf 0x3f3f3f3f
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef unsigned long long Ull;
int n,m;
vector<vector<int> > myV(20);
int res=0;
bool dp[20][1<<20];
void dfs(int step,int field){
if(step==n){
res++;
return;
}
if(dp[step][field]>0){
res+=dp[step][field];
return;
}
for(int i=0;i<myV[step].size();i++){
if(field>>(myV[step][i]-1)&1){
int t1=res;
dfs(step+1,field&~(1<<(myV[step][i]-1)));
if(res>t1){
//记忆化，到了这一步之后他有多少种组合方案，就不必再一直dfs到末尾了
dp[step][field]=res-t1;
}
}
}
}
int main() {
while(scanf("%d%d",&n,&m)!=EOF){
int size;
res=0;
for(int i=0;i<n;i++){
scanf("%d",&size);
myV[i].resize(size);
for(int j=0;j<size;j++){
scanf("%d",&myV[i][j]);
//cout<<myV[i][j]<<endl;
}
}
memset(dp,0,sizeof(dp));
dfs(0,(1<<m)-1);
printf("%d\n",res);
}
return 0;
}



## AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
int field[30][30];
int dp[(1<<20)+10];
int n,m;
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
int size;
int res=0;
memset(dp,0,sizeof(dp));
memset(field,0,sizeof(field));
for(int i=0; i<n; i++) {
scanf("%d",&size);
int x;
for(int j=0; j<size; j++) {
scanf("%d",&x);
field[i][--x]=1;
}
}

dp[0]=1;
for(int i=0;i<n;i++){
for(int j=(1<<m)-1;j>=0;j--){
if(dp[j]!=0){
for(int k=0;k<m;k++){
if(j>>k&1||!field[i][k]){
continue;
}
dp[j|1<<k]+=dp[j];
}
dp[j]=0;
}
}
}
for(int i=0;i<1<<m;i++){
res+=dp[i];
}

printf("%d\n",res);
}
return 0;
}


07-30 148
08-04 263

12-16 60
06-25 133
12-31 187
05-20 148
03-27 170
04-28 353
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试