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台电脑,现在有N种服务,现在你可以在每台电脑终止一项服务,他和他相邻的电脑都会被关闭,如果一项服务在所有电脑都没运行,该项服务成功被破坏,问最多能破坏几种服务。
思路:先把每个点集能覆盖到的电脑cover预处理出来。然后枚举每个状态,枚举每个状态的子集,如果该子集能覆盖到全部,状态转移就+1。
状态转移方程 dp[state] = dp[state - substate] + (substate == ((1<<n) - 1));
代码:
#include <stdio.h>
#include <string.h>
#define max(a,b) ((a)>(b)?(a):(b))
const int N = 17;
const int MAXN = (1<<17);
int n, point[N], cover[MAXN], dp[MAXN];
void init() {
int num, to, i;
memset(dp, 0, sizeof(dp));
memset(point, 0, sizeof(point));
memset(cover, 0, sizeof(cover));
for (i = 0; i < n; i++) {
scanf("%d", &num);
point[i] = (point[i]|(1<<i));
while (num--) {
scanf("%d", &to);
point[i] = (point[i]|(1<<to));
}
}
for (i = 0; i < (1<<n); i++) {
for (int j = 0; j < n; j++) {
if (i&(1<<j)) {
cover[i] = (cover[i]|point[j]);
}
}
}
}
int solve() {
for (int i = 0; i < (1<<n); i++) {
for (int j = i; j > 0; j = (j - 1)&i) {
if (cover[j] == (1<<n) - 1) {
dp[i] = max(dp[i], dp[i^j] + 1);
}
}
}
return dp[(1<<n) - 1];
}
int main() {
int cas = 0;
while (~scanf("%d", &n) && n) {
init();
printf("Case %d: %d\n", ++cas, solve());
}
return 0;
}
UVA 11825 - Hackers' Crackdown(dp+状态压缩)
原文:http://blog.csdn.net/accelerator_/article/details/19213533