zoukankan      html  css  js  c++  java
  • poj 2441 Arrange the Bulls(状态压缩dp)

    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

     
    状态压缩 + 滚动数组 + 位运算
     
     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
    ",ans);
    39     }
    40     return 0;
    41 }
    View Code
     
  • 相关阅读:
    android flash air 打包工具
    MyEclipse 7.0快捷键大全
    android mp4 videoView
    位图处理
    myeclise10.0下载
    dropdown.js
    back track
    ANE 跨平台 as3 转 objectc android desktop
    jquery 例子
    android 屏蔽 home 2
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4767737.html
Copyright © 2011-2022 走看看