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)只选一个女人
2)选和这个女人有关系的一群女人
于是背包最外层是关系数,即联通块的个数,次外层是背包大小,内层是联通个数(由于选择的要求在一个联通块中
只能选择一个或者全选所以背包大小要放在联通个数外面,毕竟只要选一次)然后就没然后了
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int M = 1010;
const int inf = 0X3f3f3f3f;
int wi[M] , be[M];
vector<int> vc , fr[M];
long long dp[M];
int temp , counts;
int fa[M] , n , m , w;
void init() {
for(int i = 0 ; i <= n ; i++) {
fa[i] = i;
}
}
int getf(int x) {
if(x != fa[x])
fa[x] = getf(fa[x]);
return fa[x];
}
void Union(int x , int y) {
int xx = getf(x);
int yy = getf(y);
if(xx != yy) {
fa[xx] = yy;
}
}
void dfs(int pos) {
for(int i = 1 ; i <= n ; i++) {
int father = getf(i);
if(father == pos) {
fr[temp].push_back(i);
}
}
}
int main() {
cin >> n >> m >> w;
init();
for(int i = 1 ; i <= n ; i++) {
cin >> wi[i];
}
for(int i = 1 ; i <= n ; i++) {
cin >> be[i];
}
for(int i = 1 ; i <= m ; i++) {
int x , y;
cin >> x >> y;
Union(x , y);
}
memset(dp , 0 , sizeof(dp));
temp = 0;
for(int i = 1 ; i <= n ; i++) {
if(fa[i] == i) {
vc.push_back(i);
}
}
int L = vc.size();
for(int i = 0 ; i < L ; i++) {
temp++;
dfs(vc[i]);
}
for(int i = 1 ; i <= temp ; i++) {
int len = fr[i].size();
int sum1 = 0 , sum2 = 0;
for(int j = 0 ; j < len ; j++) {
int p = fr[i][j];
sum1 += wi[p];
sum2 += be[p];
}
for(int l = M - 2 ; l >= 0 ; l--) {
if(l + sum1 < M) {
dp[l + sum1] = max(dp[l + sum1] , dp[l] + sum2);
}
for(int j = 0 ; j < len ; j++) {
int p = fr[i][j];
if(l + wi[p] < M) {
dp[l + wi[p]] = max(dp[l + wi[p]] , dp[l] + be[p]);
}
}
}
}
long long MAX = 0;
for(int i = 0 ; i <= w ; i++) {
MAX = max(MAX , dp[i]);
}
cout << MAX << endl;
return 0;
}
Codeforces Round #383 (Div. 2)D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(dp背包+并查集)的更多相关文章
- 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 ...
- 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 ...
- D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题
http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...
- 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 ...
- 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 Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...
- Codeforces Round #383 (Div. 2)C. Arpa's loud Owf and Mehrdad's evil plan
C. Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 me ...
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution —— 异或
题目链接:http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible so ...
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution
B. Arpa’s obvious problem and Mehrdad’s terrible solution time limit per test 1 second memory limit ...
随机推荐
- MySQL中一些关于索引的知识点
什么是索引 索引是一种数据结构,其作用就是用来提高数据查询效率.比较常用的比喻就是将其类比为书籍的目录.通过目录可以精确的找到某一章节的内容所在页. 在数据量较小的时候使用索引其实也没有什么意义,即使 ...
- .net持续集成测试篇之Nunit that断言
系列目录 that是Nunit的新语法,语义上不如简单断言,使用上也更加复杂,但是其功能更加强大. 其基本语法如下代码片段示: [Test] public void DemoTest() { bool ...
- Echarts图表插件(4.x版本)使用(二、带分类筛选的多个图表/实例化多个ECharts,以关系图/force为例)
导读 如果想在一个页面里实例化带分类筛选的多个Echarts该怎么做呢? 曾探讨了带分类选择的关系图显示为自定义图片的需求实现,传送门ECharts图表插件(4.x版本)使用(一.关系图force节点 ...
- maven打jar包包括依赖包
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId& ...
- Iphone使用过程中遇到的问题
Q1:同一个Apple ID不同设备之间的通话记录保持同步 解决方法: Step1:"设置"--"电话"--"在其他设备上通话"--选择关闭 ...
- Visual Studio Debug
在watch窗口输入,$err,hr可以看到上一个错误代码和相关描述信息 Error Lookup可以将错误代码转换成为相应的文本描述 FormatMessage()
- 如何编写一个WebPack的插件原理及实践
_ 阅读目录 一:webpack插件的基本原理 二:理解 Compiler对象 和 Compilation 对象 三:插件中常用的API 四:编写插件实战 回到顶部 一:webpack插件的基本原理 ...
- Go中的字符串使用----strings和strconv
Go中的字符串操作 字符串是工作中最常用的,值得我们专门的练习一下.在Go中使用strings包来操作字符串,这也是内置的包哈,不像Java中要么手写,要么引入common-lang 或者 别的第三方 ...
- H5 Handlebars的简单使用
扫码关注公众号,不定期更新干活 web 开发中,js 解析JSON 是经常的事情.非常繁琐.handlebars 使用了模版,只要你定义一个模版,提供一个json对象,handlebars 就能吧js ...
- (2019版本可用)【idea的安装,激活,设置,卸载】
前言 也差不多也可以使用简单快捷的idea软件了,相对于elicpse而言的话,idea是非常好用的,虽然现在涉及不是很广. 什么是idea? IDEA 全称IntelliJ IDEA,是用于java ...