UVA 11825 Hackers’ Crackdown(集合动态规划 子集枚举)
Hackers’ Crackdown
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 Nservices. 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
Output for Sample Input
Case 1: 3
Case 2: 2
题目大意:(黑客的攻击)假设你是一个黑客,侵入了了一个有着n台计算机(编号为0,1,…,n-1)的网络。一共有n种服务,每台计算机都运行着所有的服务。对于每台计算机,你都可以选择一项服务,终止这台计算机和所有与它相邻计算机的该项服务(如果其中一些服务已经停止,则这些服务继续处于停止状态)。你的目标是让尽量多的服务器完全瘫痪(即:没有任何计算机运行该项服务)
输入格式:输入包含多组数据。每组数据的第一行为整数n(1<=n<=16);以下n行每行描述一台计算机的相邻计算机,其中第一个数m为相邻计算机个数,接下来的m个整数位这些计算机的编号。输入结束标志为n=0。
输出格式:对于每组数据,输出完全瘫痪的服务器的最大数量。
分析:
本题的数学模型是:把n个集合p1,p2,…pn分成尽量多组,使得每组中所有集合的并集等于全集。这里的集合Pi 就是计算机 i 及其相邻计算机的集合,每组对应于题目中的一项服务。注意到n很小,可以用二进制法表示这些集合,即在代码中,每个集合Pi 实际上是一个非负整数。输入的部分代码如下:
for(int i=;i<n;i++){
int m,x;
scanf("%d",&m);
P[i] = <<i;
while(m--) { scanf("%d",&x); P[i] |= (<<x); }
}
为了方便,我们用cover(S)表示若干Pi 的集合S中所有Pi 的并集(二级制表示),即这些Pi 在数值上“按位或”。
for(int S = ; S < (<<n); S++){
cover[S] = ;
for(int i=;i<n;i++)
if(S & (<<i)) cover[S] |= P[i];
}
想到这样的动态规划:用f(S)表示子集S最多可以分成多少组,则
f(S) = max{f(S0)|S0是S的子集,cover[S0]等于全集}+1
这里有一个重要的技巧:枚举S的子集S0。详见下面代码。
int ALL = (<<n) - ;
for(int S = ;S< (<<n); S++){
f[S] = ;
for(int S0 = S; S0; S0 = (S0-)&S)
if(cover[S0] == ALL) f[S] = max(f[S], f[S^S0]+);
}
printf("Case %d: %d\n",++kase,f[ALL]);
完整代码如下:
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn = ;
int n, P[maxn], cover[<<maxn], f[<<maxn];
int main() {
int kase = ;
while(scanf("%d", &n) == && n) {
for(int i = ; i < n; i++) {
int m, x;
scanf("%d", &m);
P[i] = <<i;
while(m--) { scanf("%d", &x); P[i] |= (<<x); }
}
for(int S = ; S < (<<n); S++) {
cover[S] = ;
for(int i = ; i < n; i++)
if(S & (<<i)) cover[S] |= P[i];
}
f[] = ;
int ALL = (<<n) - ;
for(int S = ; S < (<<n); S++) {
f[S] = ;
for(int S0 = S; S0; S0 = (S0-)&S)
if(cover[S0] == ALL) f[S] = max(f[S], f[S^S0]+);
}
printf("Case %d: %d\n", ++kase, f[ALL]);
}
return ;
}
注意:位运算符的优先级比较低,注意加括号
UVA 11825 Hackers’ Crackdown(集合动态规划 子集枚举)的更多相关文章
- UVA 11825 Hackers’ Crackdown 状压DP枚举子集势
Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...
- [Uva 11825] Hackers’ Crackdown
Hackers’ Crackdown Input: Standard Input Output: Standard Output Miracle Corporations has a numbe ...
- UVA 11825 Hackers' Crackdown
题目大意就是有一个图,破坏一个点同时可以破坏掉相邻点.每个点可以破坏一次,问可以完整破坏几次,点数=16. 看到16就想到状压什么的. 尝试设状态:用f[i]表示选的情况是i(一个二进制串),至少可以 ...
- UVa 11825 - Hackers' Crackdown DP, 枚举子集substa = (substa - 1)&sta 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 11825 Hackers' Crackdown (状压DP)
题意:给定 n 个计算机的一个关系图,你可以停止每台计算机的一项服务,并且和该计算机相邻的计算机也会终止,问你最多能终止多少服务. 析:这个题意思就是说把 n 台计算机尽可能多的分成一些组,使得每组的 ...
- UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集
UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...
- uva 11825 Hackers' Crackdown (状压dp,子集枚举)
题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪( ...
- 【UVA】11825 Hackers' Crackdown(状压dp)
题目 传送门:QWQ 分析 $ n<= 16 $ 显然是状压 然后搞一搞(靠着蓝书yy一下) 代码 #include <bits/stdc++.h> using namespace ...
- UVA 11205 The broken pedometer(子集枚举)
B - The broken pedometer Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
随机推荐
- Java新手入门必须掌握的30个基本概念
今天给大家推荐的文章是有关Java基本概念的,掌握好这些基本概念对学习J2SE.J2EE.J2ME都很重要,也能更好地理解Java的精髓,初学者要注意啦! ▶Java概述: 目前Java主要应用于中间 ...
- JavaScript---网络编程(9-2)--DHTML技术演示(2-2)-表格加强
对上篇博客的最后那个表格隔行高亮显示加了个功能,鼠标监听和年龄从小到大排序. 演示代码: <html> <head> <title>DHTML技术演示---表格中页 ...
- [ZETCODE]wxWidgets教程六:事件处理
本教程原文链接:http://zetcode.com/gui/wxwidgets/events/ 翻译:瓶哥 日期:2013年12月7号星期六 邮箱:414236069@qq.com 主页:http: ...
- hdoj 2074 叠筐
叠筐 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- PTA - - 06-图1 列出连通集 (25分)
06-图1 列出连通集 (25分) 给定一个有NN个顶点和EE条边的无向图,请用DFS和BFS分别列出其所有的连通集.假设顶点从0到N-1N−1编号.进行搜索时,假设我们总是从编号最小的顶点出发, ...
- JavaScript实现遮罩层
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- GA遗传算法解析
LinJM @HQU 谈及遗传算法,我首先想到的就是孟德尔的豌豆实验.当然,遗传算法实质上并不能用豌豆实验说明,豌豆实验探讨了分离定律和自由组合定律,而遗传算法所借鉴的并不是这两个定律.遗传算法,简 ...
- 基于特定领域国土GIS应用框架设计及应用
基于特定领域国土GIS应用框架 设计及应用 何仕国 2012年8月16日 摘要: 本文首先讲述了什么是框架和特定领域框架,以及与国土GIS 这个特定领 ...
- COCOS2D-X 精灵创建随笔
CCSprite类中创建Sprite的方法都是静态的: static CCSprite* create ( ) 创建一个无图片显示的精灵,可随后用 setTexture 方法设置显示图片 stati ...
- oracle5
oracle中事务处理 什么是事务 事务用于保证数据的一致性,它由一组相关的dml语句组成,该组的dml(数据操作语言:增删改,没有查询)语句要么全部成功,要么全部失败. 如:网上转账就是典型的要用事 ...