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;
    }
    

      

  • 相关阅读:
    疯子坐飞机问题
    从打开浏览器访问网址中间发生了什么(三握四挥
    Http和Https的区别
    【11】分治,旋转数组最小数字
    【12】(难&精)【DFS】矩阵中的路径
    map的几种分类
    System.setProperties idea命令注入
    centos配置静态ip
    java 异常收集
    window10 开机启动项 添加
  • 原文地址:https://www.cnblogs.com/water-full/p/4488723.html
Copyright © 2011-2022 走看看