zoukankan      html  css  js  c++  java
  • (状态压缩DP) poj 2441

    Arrange the Bulls
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 3709   Accepted: 1422

    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

     
     
    题意:
     
    有n头牛 m个帐篷,n头牛进入m个帐篷方案数
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<string>
    using namespace std;
    int n,m,mp[21][21];
    int dp[(1<<20)+2];
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d",&x);
            while(x--)
            {
                scanf("%d",&y);
                y--;
                mp[i+1][y]=1;
            }
        }
        if(n>m)
        {
            printf("0
    ");
            return 0;
        }
        dp[0]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=(1<<m)-1;j>=0;j--)
            {
                if(dp[j]==0)
                    continue;
                for(int k=0;k<m;k++)
                {
                    if((j&(1<<k))!=0)
                        continue;
                    if(mp[i+1][k]==0)
                        continue;
                    dp[j|(1<<k)]+=dp[j];
                }
                dp[j]=0;
            }
        }
        int ans=0;
        for(int i=0;i<(1<<m);i++)
            ans+=dp[i];
        printf("%d
    ",ans);
        return 0;
    }
    

      

  • 相关阅读:
    各位AS3各种验证在这里,邮箱 身份证 ...
    各位同学还在为AS3在IE透明模式下弹出新窗口而烦恼吗?
    Flash As3 通过二进制[ByteArray]判断真实的文件类型
    【A8笔记1】Alternativa 8.5.0 在Flash、Fb、Fd中的配置
    超酷光带效果
    flash 墙
    A3D CoverFlow图片展示效果
    Windows8Metro模式IE10放弃Flash的支持
    html5 控件整理
    AS3中JSON的基本应用实例
  • 原文地址:https://www.cnblogs.com/water-full/p/4488723.html
Copyright © 2011-2022 走看看