1. 程式人生 > >POJ 2441 狀壓DP

POJ 2441 狀壓DP

you 得到 memset iostream using fine put err lan

Arrange the Bulls
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 5289 Accepted: 2033

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.

Sample Input

3 4
2 1 4
2 1 3
2 2 4

Sample Output

4

Source

POJ Monthly,TN

題意:每只牛只想在特定的房間裏玩,每個房間只能有一只牛,問你能夠滿足以上條件的情況有幾種。

代碼:

 1 //#include "bits/stdc++.h"
 2 #include "cstdio"
 3 #include "map"
 4 #include "set"
 5 #include "cmath"
 6
#include "queue" 7 #include "vector" 8 #include "string" 9 #include "cstring" 10 #include "time.h" 11 #include "iostream" 12 #include "stdlib.h" 13 #include "algorithm" 14 #define db double 15 #define ll long long 16 //#define vec vector<ll> 17 #define Mt vector<vec> 18 #define ci(x) scanf("%d",&x) 19 #define cd(x) scanf("%lf",&x) 20 #define cl(x) scanf("%lld",&x) 21 #define pi(x) printf("%d\n",x) 22 #define pd(x) printf("%f\n",x) 23 #define pl(x) printf("%lld\n",x) 24 #define inf 0x3f3f3f3f 25 #define rep(i, x, y) for(int i=x;i<=y;i++) 26 const int N = 1e5 + 5; 27 const int mod = 1e9 + 7; 28 const int MOD = mod - 1; 29 const db eps = 1e-10; 30 const db PI = acos(-1.0); 31 using namespace std; 32 int f[1<<21],a[21][21]; 33 int n,m; 34 int main() 35 { 36 while(scanf("%d%d",&n,&m)==2) 37 { 38 int x; 39 memset(a,0, sizeof(a)); 40 memset(f,0, sizeof(f)); 41 for(int i=0;i<n;i++){ 42 ci(x); 43 for(int j=0;j<x;j++){ 44 int y; 45 ci(y); 46 a[i][y-1]=1; 47 } 48 } 49 if(n>m) { 50 puts("0"); 51 continue; 52 } 53 f[0]=1; 54 for(int i=0;i<n;i++){//枚舉n只牛 55 for(int j=(1<<m)-1;j>=0;j--){//枚舉每一個集合 56 if(f[j])//如果集合存在 57 { 58 for(int k=0;k<m;k++){//枚舉每個房間 59 if((j&(1<<k))!=0||!a[i][k]) continue;//集合已用此房間或牛不喜歡此房間 60 int S=(1<<k)|j;//牛進入房間得到新的集合 61 f[S]+=f[j]; 62 } 63 } 64 f[j]=0;//枚舉的集合變為新集合,舊集合需要消除 65 } 66 } 67 ll ans=0; 68 for(int i=0;i<(1<<m);i++) ans+=f[i];//留下滿足條件的 69 pl(ans); 70 } 71 return 0; 72 }

POJ 2441 狀壓DP