Problem H
Hackers’ Crackdown
Input: Standard Input
Output: Standard Output
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 | Output for Sample Input |
3 2 1 2 2 0 2 2 0 1 4 1 1 1 0 1 3 1 2 0 | Case 1: 3 Case 2: 2 |
题意:有n台计算机的网络编号为0~n-1然后每一台计算机都进行着n种一样的服务。黑客想进行攻击
他每在一台计算机上放病毒就会令本机和相邻的机器在这种病毒针对的服务瘫痪。然后问你,黑客在每一台机
下病毒他最多可以使多少种服务进行瘫痪。没有任何计算机进行这种服务
这道题我其实是看白书有感而写。他的状态压缩实在是太巧妙了
进行了两次压缩。震惊了我。所以虽然不是我想出来的。但是觉得对自己很有用,所以记录一笔。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int P[20],cover[(1<<16)+4];
int dp[(1<<16)+4];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int n,ca = 1,m,i,j,v;
while(scanf("%d",&n) && n!=0)
{
for(i=0;i<n;i++) //状压一:每放病毒在一个点,就可以引起周围的点变化
{ //将这一点和周围的点压缩全变为1,例如:P[0] = 1101代表在0位置放病毒产生的
scanf("%d",&m); //后果是3,2,0位置一起变有病毒。
P[i] = 1<<i;
for(j=0;j<m;j++)
{
scanf("%d",&v);
P[i]|=1<<v;
}
}
v = (1<<n);
for(i=1;i<v;i++) //状压二:在哪几个点放病毒。然后结果是在哪几个点有病毒
{ //比如cover[3] = 11011代表在0和1点放病毒(3==11)最后有病毒的点是0,1,3,4点。
cover[i] = 0;
for(j=0;j<n;j++)
{
if(i&(1<<j)) cover[i]|=P[j];
}
}
memset(dp,0,sizeof(dp));
for(i=1;i<v;i++) //状态转移方程。当我i的子集j就已经可以将所有点覆盖时那么
{ //dp[i] = max(dp[i],dp[i^j]+1),i^j代表是i去除j之后的集合
for(j=i;j;j = (j-1)&i) //特别注意,枚举一个数的子集
{
if(cover[j]==v-1) //一定要是子集j已经覆盖所有条件才能进行状态转移
dp[i] = max(dp[i],dp[i^j]+1);
}
}
printf("Case %d: %d\n",ca++,dp[v-1]);
}
return 0;
}