UVA11825- Hacker’s Crackdown(枚举子集dp)

Description
Miracle Corporations has a number of system services running in a distributed computer system which is a prime target for hackers. The system is basically a set of N computer nodes with each of them running a set of N services. Note that, the set of services running on every node is same everywhere in the network. A hacker can destroy a service by running a specialized exploit for that service in all the nodes.
One day, a smart hacker collects necessary exploits for all these N services and launches an attack on the system. He finds a security hole that gives him just enough time to run a single exploit in each computer. These exploits have the characteristic that, its successfully infects the computer where it was originally run and all the neighbor computers of that node. Given a network description, find the maximum number of services that the hacker can damage.

Input
There will be multiple test cases in the input file. A test case begins with an integer N (1 ≤ N ≤ 16), the number of nodes in the network. The nodes are denoted by 0 to N − 1. Each of the following N lines describes the neighbors of a node. Line i (0 ≤ i < N) represents the description of node i. The description for node i starts with an integer m (Number of neighbors for node i), followed by m integers in the range of 0 to N − 1, each denoting a neighboring node of node i. The end of input will be denoted by a case with N = 0. This case should not be processed.

Output
For each test case, print a line in the format, ‘Case X: Y ’, where X is the case number & Y is the maximum possible number of services that can be damaged.

Sample Input
3
2 1 2
2 0 2
2 0 1
4
1 1
1 0
1 3
1 2
0
Sample Output
Case 1: 3
Case 2: 2

题意:
有n台电脑,每台电脑上都有n个相同的服务,对于每台电脑可以攻击它的一个服务,这台电脑以及和它相邻的所有电脑上的该服务都会瘫痪,问最多可以完全停掉多少服务(即所有电脑上的该服务都瘫痪了)。

解法:
这道题其实就是把每台电脑以及和它相邻的电脑看做一体的,问最多可以把这些电脑划分成多少组,每组包括了所有的电脑。
首先读入每台电脑相邻的电脑,通过|操作将该电脑和相邻的电脑的位置保存为1,存入a数组中。随后枚举所有的电脑划分组合,通过|操作将组合选中的电脑包括的所有位置都保存为1,存入cover数组中。最后枚举所有的选择,枚举子集,假如子集的cover数组包括了所有的电脑,则对f数组取max(当前个数或者去掉了该子集的个数+1),最后输出f[(1<<n)-1],注意括号。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
using namespace std;

int n,m,t=0;
int a[16+5],cover[(1<<17)+5],f[(1<<17)+5];

int main()
{
    while(scanf("%d",&n)&&n!=0){
        for(int i=0;i<n;i++){
            int x;scanf("%d",&m);
            a[i]=1<<i;
            while(m--)scanf("%d",&x),a[i]|=1<<x;
        }
        for(int s=0;s<(1<<n);s++){
            cover[s]=0;
            for(int i=0;i<n;i++)if(s&(1<<i))cover[s]|=a[i];
        }
        for(int s=0;s<(1<<n);s++){
            f[s]=0;
            for(int ss=s;ss;ss=(ss-1)&s)if(cover[ss]==(1<<n)-1)f[s]=max(f[s],f[s-ss]+1);
        }
        printf("Case %d: %d\n", ++t, f[(1<<n)-1]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值