uva11825Hackers' Crackdown

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18913

题意:《算法竞赛入门经典训练指南》算法设计基础例题29。

分析:刘汝佳讲得很好了,我就不bb了。需要学习的姿势是二进制集合子集的遍历。剩下就是状态压缩了。O(3^n)

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1000010;
const int MAX=151;
const int MOD1=1000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000009;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
int f[20],g[70010],dp[70010];
int main()
{
    int i,j,n,a,x,ca=0;
    while (scanf("%d", &n)&&n) {
        memset(f,0,sizeof(f));
        memset(g,0,sizeof(g));
        for (i=0;i<n;i++) {
            scanf("%d", &a);f[i]|=1<<i;
            for (j=0;j<a;j++) {
                scanf("%d", &x);
                f[i]|=1<<x;f[x]|1<<i;
            }
        }
        for (i=0;i<1<<n;i++)
            for (j=0;j<n;j++)
            if (i&(1<<j)) g[i]|=f[j];
        for (i=0;i<1<<n;i++) {
            dp[i]=0;
            for (j=i;j;j=(j-1)&i)
            if (g[j]==(1<<n)-1) dp[i]=max(dp[i],dp[i^j]+1);
        }
        printf("Case %d: %d\n", ++ca, dp[(1<<n)-1]);
    }
    return 0;
}


你可能感兴趣的:(uva11825Hackers' Crackdown)