zoukankan      html  css  js  c++  java
  • Arrange the Bulls POJ

    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

    dp[S]:=在S状态下安置牛的方案数。
     1 #include<bitset>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 int n,m,dp[1<<21];
     9 bool vis[21][21];
    10 
    11 void Init(){
    12     memset(dp,0,sizeof(dp));
    13     memset(vis,0,sizeof(vis));
    14 }
    15 
    16 void Read(){
    17     cin>>n>>m;
    18     int u,v;
    19     for(int i=0;i<n;i++){
    20         cin>>u;
    21         for(int j=0;j<u;j++){ cin>>v; vis[i][v-1]=true; } 
    22     }
    23 }
    24 
    25 void solve(){
    26     for(int i=0;i<m;i++) if(vis[0][i]) dp[1<<i]=1;
    27     for(int i=1;i<n;i++){
    28         for(int comb=(1<<i)-1,x,y;comb<(1<<m);x=comb&-comb,y=comb+x,comb=((comb&~y)/x>>1)|y){   //枚举集合{0,1,...,m-1}中大小为i的集合
    29             if(!dp[comb]) continue;                                                             //即是分配了i个场地的集合
    30             for(int j=0;j<m;j++)
    31                 if(vis[i][j]&&!((comb>>j)&1)) dp[comb|(1<<j)]+=dp[comb];
    32         }
    33     }
    34     int ans=0;
    35     for(int i=0;i<(1<<m);i++)
    36         if(bitset<32>(i).count()==n) ans+=dp[i];
    37     cout<<ans<<endl;
    38 }
    39 
    40 int main()
    41 {   Init();
    42     Read();
    43     solve();
    44     return 0;
    45 }
  • 相关阅读:
    HDU-3336-Count the string(扩展KMP)
    洛谷-P3805-Manacher模板
    洛谷-p5410-扩展KMP模板
    HDU-2594-Simpsons' Hidden Talents(kmp, 扩展kmp)
    POJ-3080-Blue jeans(KMP, 暴力)
    [办公应用]两个单独的列数据快速变为两列匹配关联数据
    [办公应用]如何将单词中的部分字母加下划线
    [办公应用]word 2007:全屏快捷键,让复制图片保持原样大小(office 全屏快捷键)
    [办公自动化]如何让excel图表标签中显示最新值数据
    [计算机故障处理]无法访问网络共享资源
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7388508.html
Copyright © 2011-2022 走看看