UVa 11825 - Hackers' Crackdown DP, 枚举子集substa = (substa - 1)&sta 难度: 2
题目
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2925
题意
n个节点,每个节点都有完全相同的n项服务。
每次可以选择一个节点,破坏该节点和相邻节点的某项服务。
问最多能完全破坏多少服务?
思路
如刘书,
直接枚举状态的子集
注意元素个数为k的集合有C^k_n个子集,那么枚举的时间复杂度为sum{c^k_n * 2^k} = 3^n,当n=16时,3^n=4e7,可以承受。
注意枚举子集可以通过substa = (substa - 1)&sta来做,子集的补集则为substa ^ sta。
感想
1. 一开始觉得枚举时间是2^2n,觉得不行,还是缺乏细致的计算
代码
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef pair<int, int> MyPair;
const int MAXN = ;
const int MAXSTA = << ;
int edges[MAXN][MAXN];
int edgeCnt[MAXN];
int vis[MAXN];
int vis2[MAXSTA];
int dp[MAXSTA]; int main() {
#ifdef LOCAL_DEBUG
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
//freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
int n;
for (int ti = ; scanf("%d", &n) == && n; ti++) {
for (int i = ; i < n; i++) {
scanf("%d", edgeCnt + i);
for (int j = ; j < edgeCnt[i]; j++) {
scanf("%d", edges[i] + j);
}
}
int maxsta = ( << n) - ;
for (int sta = ; sta <= maxsta; sta++) {
memset(vis, , sizeof vis);
for (int i = ; i < n; i++) {
if (sta & ( << i)) {
vis[i] = true;
for (int j = ; j < edgeCnt[i]; j++) {
vis[edges[i][j]] = true;
}
}
}
bool fl = true;
for (int i = ; i < n; i++) {
if (!vis[i]) {
fl = false;
}
}
if (fl) {
dp[sta] = ;
}
else {
dp[sta] = ;
} }
for (int sta = ; sta <= maxsta; sta++) {
for (int subSta = sta; subSta != ; subSta = (subSta - ) & sta) {
dp[sta] = max(dp[sta], dp[sta ^ subSta] + dp[subSta]);
} }
printf("Case %d: %d\n", ti, dp[maxsta]);
} return ;
}
UVa 11825 - Hackers' Crackdown DP, 枚举子集substa = (substa - 1)&sta 难度: 2的更多相关文章
- 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 Miracle Corporations has a number of system services running in a distributed com ...
- UVA 11825 Hackers' Crackdown
题目大意就是有一个图,破坏一个点同时可以破坏掉相邻点.每个点可以破坏一次,问可以完整破坏几次,点数=16. 看到16就想到状压什么的. 尝试设状态:用f[i]表示选的情况是i(一个二进制串),至少可以 ...
- [Uva 11825] Hackers’ Crackdown
Hackers’ Crackdown Input: Standard Input Output: Standard Output Miracle Corporations has a numbe ...
- 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 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- [Luogu P3959] 宝藏 (状压DP+枚举子集)
题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...
- uva 11825 Hackers' Crackdown (状压dp,子集枚举)
题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪( ...
随机推荐
- P10.3 usestock0.cpp
stock.h #ifndef STOCK_H #define STOCK_H #include <string> class Stock //类声明 { private: std::st ...
- 虚拟机ubuntu的常用命令集合
一.文件/文件管理 ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd -回上一 ...
- Pychram - 使用介绍
Pychram - 使用介绍 PyCharm中Directory与Python package的区别 对于Python而言,有一点是要认识明确的,python作为一个相对而言轻量级的,易用的脚本语言( ...
- Memcached安装&启动
安装 *Linux系统安装memcached,首先要先安装libevent库,安装请指定 --with--libevent=PATH(若安装过程中出现configure: error : no acc ...
- Spring之Spel表达式
正常业务场景一般不用这个技术,但需要知道有这么个东西支持Spring. 记忆力不好,抄了些套路代码便于以后用到. package com.paic.phssp.springtest.spel; imp ...
- English trip V1 - B 17. Giving Information 提供信息 Teacher:Taylor Key: Person Information
In this lesson you will learn to say your phone number and address. 这节课讲学习说你的手机号码和地址. 课上内容(Lesson) ...
- English Voice of <<Dream it passible>>
Dream It Possible(梦想成为可能) - DelaceyI will run I will climb I will soar.我奔跑,我攀爬 我要飞翔.I'm undefeated我所 ...
- 在Java、Web和移动开发方面最值得关注的12大开源框架
在这篇文章中,我将分享一些值得开发者学习的优秀框架,以提高他们在移动开发.Web 开发以及大数据方面的开发技能. 1.AngularJS 这是一个JavaScript框架,我已经把它加入到我的2018 ...
- 虚拟机linux 如何挂在U盘,NTFS格式如何挂载
今天突发奇想,想挂载U盘到虚拟机的Centos 7 上,但是出了些问题,下边我就来说下linux挂在U盘的步骤 电脑插上U盘 win + R运行 services.msc 找到虚拟机的USB服务并运行 ...
- php打印
function preview() { bdhtml = window.document.body.innerHTML; sprnstr = "<!--startprint--> ...