题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值。

  分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =。

  代码如下:

 #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const int N = + ; int w[N],b[N];
int n,m,W;
int root[N];
vector<int> v[N];
int allw[N],allb[N];
int dp[N];
int find(int x) {return x == root[x] ? x : root[x] = find(root[x]);} int main()
{
scanf("%d%d%d",&n,&m,&W);
for(int i=;i<=n;i++) root[i] = i;
for(int i=;i<=n;i++) scanf("%d",w+i);
for(int i=;i<=n;i++) scanf("%d",b+i);
for(int i=;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
int rx = find(x), ry = find(y);
if(rx != ry) root[rx] = ry;
}
for(int i=;i<=n;i++)
{
int t = find(i);
v[t].push_back(i);
allw[t] += w[i];
allb[t] += b[i];
}
for(int i=;i<=n;i++)
{
if(v[i].size() == ) continue;
for(int j=W;j>=;j--)
{
for(int k=;k<v[i].size();k++)
{
if(j-w[v[i][k]] >= ) dp[j] = max(dp[j], dp[j-w[v[i][k]]] + b[v[i][k]]);
}
if(j-allw[i] >= ) dp[j] = max(dp[j], dp[j-allw[i]] + allb[i]);
}
}
printf("%d\n",dp[W]);
return ;
}

codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)的更多相关文章

  1. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(分组背包+dsu)

    D. Arpa's weak amphitheater and Mehrdad's valuable Hoses Problem Description: Mehrdad wants to invit ...

  2. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

    [题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...

  3. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)

    <题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...

  4. D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题

    http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...

  5. Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses —— DP(01背包)

    题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...

  6. Arpa's weak amphitheater and Mehrdad's valuable Hoses

    Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit per ...

  7. B. Arpa's weak amphitheater and Mehrdad's valuable Hoses

    B. Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit p ...

  8. 【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. Codeforces 741B:Arpa's weak amphitheater and Mehrdad's valuable Hoses(01背包+并查集)

    http://codeforces.com/contest/741/problem/B 题意:有 n 个人,每个人有一个花费 w[i] 和价值 b[i],给出 m 条边,代表第 i 和 j 个人是一个 ...

随机推荐

  1. Linux 忘记root密码 的解决办法

    以单用户维护模式登录 先将系统重启, 在读秒时按下任意键进入菜单界面,再仔细看菜单下的说明,按下e就能进入grub的编辑模式,如下 将光标移动到kernel那行, 再次按e进入kernel的编辑界面中 ...

  2. HTTP返回码总结(转)

    HTTP协议状态码表示的意思主要分为五类 ,大体是 :   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~  1×× 保留   2×× 表示请求成功地接收   3×× 为完成请求客户需进一步 ...

  3. vim常用操作

    vim filename 编辑一个文件 在一般模式里按yy是复制的意思(复制当前行),按yy之前先按相应的数字键就是复制光标所在行到指定的行,然后按p粘贴在一般模式里按dd是删除的意思(也叫做剪切), ...

  4. ORDER BY 1,2 desc

    ORDER BY 1,2 desc     --先按照选择列里的第一列进行升序排序,再按选择列的第二列降序排序    --选择列是指查询语句中select后面跟着的字段(1,2は検索の列)

  5. [AS3.0] NetConnection.Connect.Rejected 解决办法

    以下是运用FMS录制视频的一段代码: package { import flash.display.Sprite; import flash.events.AsyncErrorEvent; impor ...

  6. Python format格式化输出

    http://www.jb51.net/article/63672.htm 推荐参考 >>> '{0},{1}'.format('hello','python') 'hello,py ...

  7. MySQL For Windows修改最大连接数

    1.从官网下载安装MySQL Installer.MySQL Installer 提供了简单易用.向导式的 MySQL 软件的安装体验过程(目前只支持 Windows),包含的产品有: MySQL S ...

  8. Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询

    最近项目中要用到模糊查询,开始研究lucene,期间走了好多弯路,总算实现了一个简单的demo. 使用的lucene jar包是3.6版本. 一:建立数据库表,并加上测试数据.数据库表:UserInf ...

  9. IIS启用兼容模式设置(win2k3—Win7)

    点击添加按钮(上图),弹出下面的对话框(下图).在自定义HTTP头名处输入: X-UA-compatible 在自定义HTTP头值处输入: IE=EmulateIE7 (输入时注意不要留下空格)输入完 ...

  10. 查询oracle连接数 对应的 应用程序

     select  b.MACHINE, b.PROGRAM , count(*) from v$process a, v$session b where a.ADDR = b.PADDR and  b ...