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 ...
随机推荐
- POJ2482 Stars in Your Window(扫描线+区间最大+区间更新)
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I stil ...
- jenkins构建maven项目
使用jenkins构建部署maven项目 因为一开始我们是自定义插件,所以构建项目的时候没有显示maven风格的项目.如果要想使用maven,我们这里必须得安装一下插件,我们在插件管理器中, 可以看到 ...
- [USACO 2011 Dec Gold] Threatening Letter【后缀】
Problem 3: Threatening Letter [J. Kuipers, 2002] FJ has had a terrible fight with his neighbor and w ...
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- Plugging an Unplugged Pluggable Database issue 3
Multitenant Unplug/Plug Best Practices (文档 ID 1935365.1) 1.source 从0419 升级到1019 ,但是datapatch 没有回退041 ...
- [转]Monkey测试简介
转自:http://www.cnblogs.com/manuosex/p/3215270.html 在android手机上做自动化测试,monkey比cts,Android UnitTest 好用多了 ...
- ambari-server启动WARN qtp-ambari-client-87] ServletHandler: 563 /api/v1/stacks/HDP/versions/2.4/recommendations java.lang.NullPointerException报错解决办法(图文详解)
问题详情 来源是,我在Ambari集群里,安装Hue. 给Ambari集群里安装可视化分析利器工具Hue步骤(图文详解 所遇到的这个问题. 然后,去ambari-server的log日志,查看,如 ...
- ABP教程(一)- ABP介绍
ABP是什么 ABP是”ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应用 ...
- [BZOJ1878][SDOI2009]HH的项链 莫队
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1878 不带修改的莫队,用一个桶记录一下当前区间中每种颜色的数量就可以做到$O(1)$更新了 ...
- WP7 开发资料
前言 离Windows Phone 7正式发布已过去几个月了,但国内关于Windows Phone 7的中文书籍资料太少了,大多数是英文资料,一本真正的中文开发教程书都没有, 要啃英文资料对大部分的开 ...