Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)
题意:
给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个。
分析:
可以采用并查集将所有的集合统计出来, 然后再将集合中所有的价值总和sumb和所有的花费总和sumw也作为一个元素加入集合。然后就是分组背包的做法,目前暂时参考背包九讲。

#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int f[maxn]; //并查集
int b[maxn], w[maxn];
int n, W, m;
void init()
{
for(int i = ; i <= n; i++){
f[i] = i;
}
}
int Find(int x)
{
if( x == f[x])
return x;
else {
f[x] = Find(f[x]);
return f[x];
}
}
int dp[maxn];
void Merge(int v , int u)
{
int t1,t2;
t1 = Find(v);
t2 = Find(u);
if(t1 != t2){
f[t2] = t1;
}
}
struct s{
int w, b;
s(int _w, int _b) : w(_w), b(_b){};
};
vector<s> group[maxn];
int divgroup()
{
for(int i = ; i <= n;i++)
{
group[Find(i)].push_back(s(w[i],b[i]));
}
for(int i = ; i <= n; i++)
{
if(!group[i].empty())
{
int w_sum = , b_sum = ;
for(int j = ; j < group[i].size(); j++){
w_sum += group[i][j].w;
b_sum += group[i][j].b;
}
group[i].push_back(s(w_sum, b_sum));
}
}
}
int main()
{
#if LOCAL
freopen("1.txt","r",stdin);
#endif // LOCAL
scanf("%d %d %d", &n ,&m, &W);
for(int i = ; i <= n; i++){
scanf("%d", &w[i]);
}
for(int i = ; i<= n; i++){
scanf("%d", &b[i]);
} init();
for(int i = ; i < m; i++){
int u, v;
scanf("%d %d", &u, &v);
Merge(u,v);
}
divgroup(); for(int k = ; k <= n; k++){
if(!group[k].empty()){
for(int v = W; v >= ; v--){
for(int i = ; i < group[k].size(); i++){
if(v-group[k][i].w >= )
dp[v] = max(dp[v], dp[v-group[k][i].w]+ group[k][i].b);
}
}
}
}
printf("%d\n", dp[W]); return ;
}
Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)的更多相关文章
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
<题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses
[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 个人是一个 ...
- 【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 ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
随机推荐
- 如何验证自己的网络是否支持ipv6
http://test-ipv6.com/进入得到自己的ipv6地址 然后命令行 ping 一下
- Vue父子组件传值之——访问根组件$root、$parent、$children和$refs
Vue组件传值除了prop和$emit,我们还可以直接获取组件对象: 根组件: $root // 单一对象 表示当前组件树的根 Vue 实例,即new Vue({...根组件内容}).如果当前实例没有 ...
- spring/spring boot/spring cloud书籍推荐
最近看了一些spring书籍,主要都是工作需要,实话说,没有必要买这么多书,每个主题一本就足够了,其他的补充可以通过项目实战和上网看官网或者博客补充. 说是推荐,其实只是一些简单读后感想而已,每本书都 ...
- BOA服务器搭建步骤
1.下载Boa Webserver的源码 http://www.boa.org/ 2.解压并编译Boa Webserver $ tar xvf boa-0.94.13.tar.gz 由于Boa Web ...
- CMake学习笔记四:usb_cam的CMakeLists解析
最近在学习cmake,在完整看了<cmake实践>一书后,跟着书上例程敲了跑了一遍,也写了几篇相关读书笔记,算是勉强基本入门了.所以找了usb_cam软件包的CMakeLists.txt来 ...
- [GZOI2016] 亚索的量子实验【分块】
第二题 亚索的粒子实验 [问题描述] 亚索是一名伟大的科学家,他最近在做一个粒子的实验,粒子初始有一定的能量,实验过程中倘若第i个粒子被注入k能量,那该粒子就会增加k能量,同时由于辐射作用,第2i,3 ...
- 一个简单的公式——求小于N且与N互质的数的和
首先看一个简单的东西. 若$gcd(i,n)=1$,则有$gcd(n-i,n)=1$ 于是在小于$n$且与$n$互质的数中,$i$与$n-i$总是成对存在,且相加等于$n$. 考虑$i=n-i$的特殊 ...
- debian设置英文模式
Ubuntu太臃肿了,遂换回debian系统.在虚拟机上装debian,发现console下中文不显示. 各种export方法试过,始终无效.废了一个小时终于找到方法了.记录之. debian设置语言 ...
- eclipse 升级note
参考http://www.cnblogs.com/jiqingwu/archive/2013/05/26/eclipse_plugins_import.html. 最终决定采用 启动 eclipse. ...
- list map接口传递
1.传参时可以设置为jsonArray的格式 JSONArray array = new JSONArray(); JSONObject json = new JSONObject(); json.p ...