zoukankan      html  css  js  c++  java
  • POJ 2441 状压DP

    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

    题意:每只牛只想在特定的房间里玩,每个房间只能有一只牛,问你能够满足以上条件的情况有几种。

    代码:

     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
    ",x)
    22 #define pd(x) printf("%f
    ",x)
    23 #define pl(x) printf("%lld
    ",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 }
  • 相关阅读:
    Prime Cryptarithm
    Barn Repair
    Mixing Milk
    June Challenge 2017
    Dual Palindromes
    数学专题
    遗传算法学习
    UVA 11464 暴力+位运算 ***
    233
    hdu 3236 二维背包
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/8393717.html
Copyright © 2011-2022 走看看