题目链接 :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背包+并查集)的更多相关文章

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. python 处理json数据

    python 处理 json数据 以下是登录账号后获取的json数据,headers中注意加入cookie值 需要处理的数据如下: 全部代码如下 #!/usr/bin/env python # -*- ...

  2. 我的ubuntu kylin中mentohust的使用历程

    1首先下载mentohus 最新版下载(包括源码):http://code.google.com/p/mentohust/downloads/list 2打开终端(Ctrl+Alt+T) 输入sudo ...

  3. Java Lambda表达式forEach无法跳出循环的解决思路

    Java Lambda表达式forEach无法跳出循环的解决思路 如果你使用过forEach方法来遍历集合,你会发现在lambda表达式中的return并不会终止循环,这是由于lambda的底层实现导 ...

  4. html的一些基本语法学习与实战

    其实在学校前端开始之前,问过自己为什么要学,因为自己学的比较杂,直到现在刚刚毕业出来工作了,才明确了方向了,要往嵌入式方向走,但是随着时代的发展,在编程和智能硬件结合的越来越紧密,特别是物联网这一块, ...

  5. 昏睡了8年的我带着第一个微信小程序今年醒了

    工作8年之久的我今年算是彻底长进了,以前是知道自己的水平不咋地,但是没什么行动,理由是3年抱2娃,需要照顾孩子. 去年年底偶然一次看技术贴的时候,看到了博客园这个平台,看了很多大牛们的经历,也知道公司 ...

  6. JDK、JRE、JVM之间的区别和联系

    JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...

  7. 禅道、jenkins部署记录

    禅道部署1.检查你linux系统的位数(uname -a)2.下载对应位数的禅道包3.通过xftp工具将禅道包拷贝到虚拟机的/opt目录4.tar 对禅道包进行解压5.改配置:vi /opt/zbox ...

  8. Go中的异常处理

    1. errors包 Go 有一个预先定义的 error 接口类型 : type error interface { Error() string } 错误值用来表示异常状态.Go也提供了一个包:er ...

  9. 如何用Hexo+Github创建自己的技术博客

    注册一个github GitHub官网.按照一般的网站注册登录执行就好了,不详细说. 安装git 安装很简单,一直下一步 git安装教程 很多教程里都说要配置环境变量,我本人安装过5次左右的git,一 ...

  10. 在Vue 中使用Typescript

    Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...