<题目链接>

题目大意:

就是有n个人,每个人都有一个体积和一个价值。这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组。现在要在这些组中至多选一个人或者这一组的人都选,在总容量为W的情况下,如何使得所选人的价值总和最大。

解题分析:

很明显有朋友关系的人需要用DFS或者并查集进行分组。考虑全选这一组的情况,需要将这一组的人打包,看成一个人,然后塞入对应的组中。之后的分组背包就能够实现全选这一组的人的情况。

#include <bits/stdc++.h>
using namespace std;
template<typename T>
inline void read(T&x){
x=;int f=;char c=getchar();
while(c<'' || c>''){ if(c=='-')f=-;c=getchar(); }
while(c>='' && c<=''){ x=x*+c-'';c=getchar(); }
x*=f;
} const int N = 2e3+;
int n,m,w,fa[N],dp[N]; struct Node{ int val,col; Node(){ val=;col=; } }node[N];
vector<Node>vec[N]; int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } inline void merge(int u,int v){
if(find(u)!=find(v))
fa[find(v)]=find(u);
}
int main(){
read(n);read(m);read(w);
for(int i=;i<=n;i++)read(node[i].col),fa[i]=i;
for(int i=;i<=n;i++)read(node[i].val);
while(m--){
int u,v;read(u);read(v);
merge(u,v);
}
for(int i=;i<=n;i++){
vec[find(i)].push_back(node[i]); //对这些物品分组
}
for(int i=;i<=n;i++){
if(find(i)!=i)continue;
Node nowsum;
for(int j=;j<vec[i].size();j++){
nowsum.val+=vec[i][j].val;
nowsum.col+=vec[i][j].col;
}
vec[i].push_back(nowsum); //将整组的看成一个物品,后面分组背包的时候就能够直接考虑全选这一整组物品的情况
}
for(int k=;k<=n;k++){ //分组背包
if(find(k)!=k)continue;
for(int v=w;v>=;v--){
for(int i=;i<vec[k].size();i++){
Node now=vec[k][i];
if(v-now.col<)continue;
dp[v]=max(dp[v],dp[v-now.col]+now.val);
}
}
}
printf("%d\n",dp[w]);
}

Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)的更多相关文章

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

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

  2. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

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

    题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...

  4. 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 ...

  5. 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 ...

  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. 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 个人是一个 ...

  9. Codeforces Round #383 (Div. 2)D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(dp背包+并查集)

    题目链接 :http://codeforces.com/contest/742/problem/D 题意:给你n个女人的信息重量w和美丽度b,再给你m个关系,要求邀请的女人总重量不超过w 而且如果邀请 ...

随机推荐

  1. python之路(11)描述符

    前言 描述符是用于代理另一个类的属性,一般用于大型的框架中,在实际的开发项目中较少使用,本质是一个实现了__get__(),__set__(),__delete__()其中一个方法的新式类 __get ...

  2. C++数组的初始化

    来源:https://zhidao.baidu.com/question/380723280.html int a[]={1,2,3}; 这种方式初始化,大括号里写了几个元素那么数组里就有几个元素,相 ...

  3. NPOI 读取excel的时候,时间格式的处理

    excel的时间格式是:CellType.Numeric 要判断时间还需要方法:DateUtil.IsCellDateFormatted(cell)的帮助: 示例代码如下: ICell cell = ...

  4. JAVA之简单编程练习

    1.有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? 解决思路:递归方法解决,兔子的规律为数列1,1,2,3,5,8 ...

  5. 机器学习基石12-Nonlinear Transformation

    注: 文章中所有的图片均来自台湾大学林轩田<机器学习基石>课程. 笔记原作者:红色石头 微信公众号:AI有道 上一节课介绍了分类问题的三种线性模型,可以用来解决binary classif ...

  6. Top K Frequent Words

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  7. 两个MMCM共享时钟输入时的严重警告和错误

    情景描述: 芯片:zynq7020 问题: 设计从FPGA的U19引脚上的开发板板接收时钟输入125M,并将其送到两个MMCM.使用软件:vivado2015.4在Vivado中打开合成设计后,我得到 ...

  8. 添加一个非模态对话框在revit中

    RequestHandler handler = new RequestHandler(); ExternalEvent exEvent = ExternalEvent.Create(handler) ...

  9. 简单python接口测试编写和django开发环境的搭建

    安装django环境 启动django D:\python\imooc>python manage.py runserver 0.0.0.0:8000 命令行下django新建app D:\py ...

  10. jQuery基础操作

    1.jQuery的介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库.jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交 ...