题目

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的更多相关文章

  1. UVA 11825 Hackers’ Crackdown 状压DP枚举子集势

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...

  2. UVA 11825 Hackers’ Crackdown(集合动态规划 子集枚举)

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...

  3. UVA 11825 Hackers' Crackdown

    题目大意就是有一个图,破坏一个点同时可以破坏掉相邻点.每个点可以破坏一次,问可以完整破坏几次,点数=16. 看到16就想到状压什么的. 尝试设状态:用f[i]表示选的情况是i(一个二进制串),至少可以 ...

  4. [Uva 11825] Hackers’ Crackdown

    Hackers’ Crackdown  Input: Standard Input Output: Standard Output   Miracle Corporations has a numbe ...

  5. UVa 11825 Hackers' Crackdown (状压DP)

    题意:给定 n 个计算机的一个关系图,你可以停止每台计算机的一项服务,并且和该计算机相邻的计算机也会终止,问你最多能终止多少服务. 析:这个题意思就是说把 n 台计算机尽可能多的分成一些组,使得每组的 ...

  6. UVA 11825 - Hackers&#39; Crackdown 状态压缩 dp 枚举子集

    UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集 ACM 题目地址:option=com_onlinejudge&Itemid=8&page=sh ...

  7. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  8. [Luogu P3959] 宝藏 (状压DP+枚举子集)

    题面 传送门:https://www.luogu.org/problemnew/show/P3959 Solution 这道题的是一道很巧妙的状压DP题. 首先,看到数据范围,应该状压DP没错了. 根 ...

  9. uva 11825 Hackers&#39; Crackdown (状压dp,子集枚举)

    题目链接:uva 11825 题意: 你是一个黑客,侵入了n台计算机(每台计算机有同样的n种服务),对每台计算机,你能够选择终止一项服务,则他与其相邻的这项服务都终止.你的目标是让很多其它的服务瘫痪( ...

随机推荐

  1. git批量删除文件和批量提交

    1. 单个删除文件: ① 通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:(可选操作,可直接执行②删除) $ rm test.txt ② 确实要从版本库中删除该文件,那就用命令git rm ...

  2. vuex学习与实践——mapState、getter、mapGetters

    1.mapState辅助函数 当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余.为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键 ...

  3. Flask请求上下文request

  4. HTML标记 2 ——表格

    <table width="800" border="0">     -----------------------表开头 <tr>   ...

  5. Mac python 环境配置

    问题:mac 只带了python2.7,要想使用高版本的Python,如python3.x,只能再次安装了,这样就会遇到 两个版本的切换问题了 如下图 : 执行 which python 如下图,查看 ...

  6. SublimeText3按ctrl+b执行python无反应

    现象:在Sublime中打开.py文件,按”ctrl+b”执行时无反应.点击工具->编译系统中已经有且识别到Python,但执行”run(ctrl+shift+b)”时无反应,Sublime左下 ...

  7. 书法字帖 PDF转化为可打印PDF

    书法类的PDF,因为底色是黑色的,打印起来特别费墨,所以需要转化成白底黑字的文件, 才好打印. 1)用 pdfbox 的 ExtractImages 命令,抽出所有的图片 https://pdfbox ...

  8. ubuntu 下Visual Studio Code 安装

    Build in Visual Studio Code Install VSCode The easiest way to install for Debian/Ubuntu based distri ...

  9. tstringlist详细用法

    TStringList 类是在Delphi使用最厂的一个对像,我们这里一起来看看 TStringList 的详细用法. 先把要讨论的几个属性列出来:1.CommaText2.Delimiter &am ...

  10. 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形网格惰性加载节点

    jQuery EasyUI 树形菜单 - 树形网格惰性加载节点 有时我们已经得到充分的分层树形网格(TreeGrid)的数据. 我们还想让树形网格(TreeGrid)按层次惰性加载节点. 首先,只加载 ...