http://www.cnblogs.com/acm-bingzi/p/3272898.html

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=0;i<n;i++){
int m,x;
scanf("%d",&m);
P[i] = 1<<i;
while(m--) { scanf("%d",&x); P[i] |= (1<<x); }
}

  为了方便,我们用cover(S)表示若干Pi 的集合S中所有Pi 的并集(二级制表示),即这些Pi 在数值上“按位或”。

for(int S = 0; S < (1<<n); S++){
cover[S] = 0;
for(int i=0;i<n;i++)
if(S & (1<<i)) cover[S] |= P[i];
}

想到这样的动态规划:用f(S)表示子集S最多可以分成多少组,则

  f(S) = max{f(S-S0)+1 |S0是S的子集,cover[S0]等于全集}

如何理解这个方程呢?

S0是S的子集,并且S0可以覆盖全部的点,也就是说集合S0至少可以分成一组

把S0分成一组,剩下的是集合S-S0,最多可以分成f(S-S0)组

选出其中最大的,再加上S0的一组,就是f(S)

这里有一个重要的技巧:枚举S的子集S0。详见下面代码。

int ALL = (1<<n) - 1;
for(int S = 1 ;S< (1<<n); S++){
f[S] = 0;
for(int S0 = S; S0; S0 = (S0-1)&S)
if(cover[S0] == ALL) f[S] = max(f[S], f[S^S0]+1);
}
printf("Case %d: %d\n",++kase,f[ALL]);

完整代码如下:

 1 #include<cstdio>
2 #include<algorithm>
3 using namespace std;
4
5 const int maxn = 16;
6 int n, P[maxn], cover[1<<maxn], f[1<<maxn];
7 int main() {
8 int kase = 0;
9 while(scanf("%d", &n) == 1 && n) {
10 for(int i = 0; i < n; i++) {
11 int m, x;
12 scanf("%d", &m);
13 P[i] = 1<<i;
14 while(m--) { scanf("%d", &x); P[i] |= (1<<x); }
15 }
16 for(int S = 0; S < (1<<n); S++) {
17 cover[S] = 0;
18 for(int i = 0; i < n; i++)
19 if(S & (1<<i)) cover[S] |= P[i];
20 }
21 f[0] = 0;
22 int ALL = (1<<n) - 1;
23 for(int S = 1; S < (1<<n); S++) {
24 f[S] = 0;
25 for(int S0 = S; S0; S0 = (S0-1)&S)
26 if(cover[S0] == ALL) f[S] = max(f[S], f[S^S0]+1);
27 }
28 printf("Case %d: %d\n", ++kase, f[ALL]);
29 }
30 return 0;
31 }

注意:位运算符的优先级比较低,注意加括号

黑客攻击 UVa11825的更多相关文章

  1. Avast!:小型网站最易遭受的3种黑客攻击

    avast是捷克研发的杀毒软件,从网站上找到一篇avast关于网站安全的文章,觉得颇有意思,因此想到翻译过来与大家共享.有不对之处还望大家批评指正. 一个拥有上万访问者的小型网站管理员发来一份信,向我 ...

  2. CTF---安全杂项入门第三题 这是捕获的黑客攻击数据包,Administrator用户的密码在此次攻击中泄露了,你能找到吗?

    这是捕获的黑客攻击数据包,Administrator用户的密码在此次攻击中泄露了,你能找到吗?分值:30 来源: 2014sctf 难度:难 参与人数:3918人 Get Flag:384人 答题人数 ...

  3. 3种不走寻常路的黑客攻击泄露&如何保护自己?

    数据泄露和黑客攻击现在已经成为我们日常生活中的常见部分,除非您是网络安全专业人员或您的个人数据受到威胁,否则您实际上并不关心是否存在新的漏洞. 正如美国联邦贸易委员会指出的那样,万豪酒店连锁店的超过5 ...

  4. TLS漏洞:超过50万个电子邮件服务器容易受黑客攻击,太可怕了

    2019年在流行的开源Exim电子邮件服务器软件中发现了一个关键的远程执行代码漏洞,至少有超过50万个电子邮件服务器容易受到远程黑客攻击.Exim是一种广泛使用的开源邮件传输代理(MTA)软件,为类似 ...

  5. 伊朗Cisco路由器遭黑客攻击 全国互联网几乎瘫痪

    2018年4月9日,黑客攻击了伊朗的国家信息数据中心.伊朗internet信息安全部称,此次大规模袭击影响了全球约二十万个思科Cisco路由交换器,也包括伊朗的几千个路由器.攻击也影响了互联网服务供应 ...

  6. Bugku-CTF分析篇-weblogic(黑客攻击了Weblogic应用,请分析攻击过程,找出Weblogic的主机名。)

    weblogic 黑客攻击了Weblogic应用,请分析攻击过程,找出Weblogic的主机名. flag格式:flag{} Tip:主机名为十六进制.  

  7. 新手学黑客攻防-黑客攻击电脑方式和认识IP地址

    听说过黑客,没见过黑客,从最基础的开始学习,让我能在互联网中保护自己的隐私安全和信息安全. 黑客攻击电脑方式 黑客攻击的方式多种多样,但常见的只有以下几种,基本上每个黑客都会用到: 网络报文嗅探 网络 ...

  8. 拆招黑客!github代码库大牛们如何应对黑客攻击

    2019年05月,<个人电脑杂志>网站报道,GitHub(2018年被微软收购)代码库正遭到一名黑客的入侵(392个资源库受损,约1000名用户受到攻击,真实资料未知).据称,这名黑客先擦 ...

  9. 接口自动化测试的"开胃小菜"---简单黑客攻击手段

    Web应用系统的小安全漏洞及相应的攻击方式 接口自动化测试的"开胃小菜" 1   写作目的 本文讲述一个简单的利用WebAPI来进行一次基本没有破坏力的“黑客”行为. 主要目的如下 ...

随机推荐

  1. 1102 Invert a Binary Tree

    题意:给定一个二叉树,要求输出翻转后的二叉树的层序序列和中序序列. 思路:不用真的翻转,只需要在输出时先访问右结点再访问左结点即可. 代码: #include <cstdio> #incl ...

  2. abbreviation

    1. ps------process status 2. tty-----teletype 3. ping----packet internet groper 4. nohup-----no hang ...

  3. USB驱动程序之USB总线驱动程序学习笔记

    USB总线驱动程序的作用 1. 识别USB设备 1.1 分配地址 1.2 并告诉USB设备(set address) 1.3 发出命令获取描述符 描述符的信息可以在include\linux\usb\ ...

  4. Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入(1)

    一.本系列分为6部分 1.Centos 7.0 下安装 Zabbix server 3.0服务器的安装及 监控主机的加入 2.Centos 6.5 下安装 Zabbix server 3.0服务器的安 ...

  5. 【转】C#中Func与Action的理解

    原文地址:https://www.cnblogs.com/ultimateWorld/p/5608122.html Action 与 Func是.NET类库中增加的内置委托,以便更加简洁方便的使用委托 ...

  6. SVN 提交、更新、解决冲突等操作步骤

    1. 纳入版本控制 ①新建文件abc.txt ②在文件上点右键 ③添加后文件图标发生变化 2. 提交 ①使用TortoiseSVN可以提交具体某一个文件,或某一个目录下的所有改变.方法就是在想要提交的 ...

  7. CSS 父级方法清除浮动方法

    .baseMod:after { clear: both; content: ' '; display: block; visibility: none; height: 1% } 2. overfl ...

  8. 「小程序JAVA实战」小程序的基础组件(24)

    转自:https://idig8.com/2018/08/12/xiaochengxu-chuji-24/ 来说下 ,小程序的基础组件.源码:https://github.com/limingios/ ...

  9. 对称加密——对入参进行DES加密处理

    体验更优排版请移步原文:http://blog.kwin.wang/programming/symmetric-encryption-des-js-java.html 对称加密是最快速.最简单的一种加 ...

  10. 7. H.264的句法和语义

    1.句法 在编码器输出的码流中,数据的基本单位是句法元素,每个句法元素由若干比特组成,它表示某个特定的物理意义,例如:宏块类型.量化参数等. 句法表征句法元素的组织结构,语义阐述句法元素的具体含义. ...