码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2441 Arrange the Bulls(状态压缩dp)

时间:2015-08-28 21:12:17      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

Description

Farmer Johnsons 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

 
状态压缩 + 滚动数组 + 位运算
 
技术分享
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define N 26
 6 int like[N][N];
 7 int dp[2][1<<20];
 8 int main()
 9 {
10     int n,m;
11     while(scanf("%d%d",&n,&m)==2){
12 
13         for(int i=1;i<=n;i++){
14             int s;
15             scanf("%d",&s);
16             for(int j=1;j<=s;j++){
17                 scanf("%d",&like[i][j]);
18             }
19         }
20 
21         dp[0][0]=1;
22         for(int i=1;i<=n;i++){
23             for(int j=0;j<(1<<m);j++){
24                 if(dp[(i-1)&1][j]){
25                     for(int k=1;k<=m;k++){
26                         if(like[i][k] && (j&(1<<(like[i][k]-1)))==0 ){
27                             dp[i&1][j|(1<<(like[i][k]-1))]+=dp[(i-1)&1][j];
28                         }
29                     }
30                 }
31             }
32             memset(dp[(i-1)&1],0,sizeof(dp[(i-1)&1]));
33         }
34         int ans=0;
35         for(int i=0;i<(1<<m);i++){
36             ans+=dp[n&1][i];
37         }
38         printf("%d\n",ans);
39     }
40     return 0;
41 }
View Code

 

 

poj 2441 Arrange the Bulls(状态压缩dp)

标签:

原文地址:http://www.cnblogs.com/UniqueColor/p/4767737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!