题目链接 :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. TCP拥塞算法瓶颈及TCP加速器解决方案

    TCP拥塞算法详解    ps:详解TCP拥塞算法就是为了说明瓶颈所在.   先解释一下概念: 拥塞:对网络中某一资源的需求超出了该资源所能提供的可用部分 拥塞窗口:以字节为单位,表示能通过的数据报的 ...

  2. 完全零基础在Linux中安装 JDK

    完全零基础在Linux中安装 JDK 总体思路:先确定没有Java程序了 — 然后创建相应路径文件夹 — 下载JDK — 解压到当前路径 — 自定义文件名称 — 配置环境变量 — 检查是否安装成功 第 ...

  3. Selenium+java - 借助autolt完成上传文件操作

    写在前面: 上传文件是每个自动化测试同学会遇到,而且可以说是面试必考的问题,标准控件我们一般用sendkeys()就能完成上传,但是我们的测试网站的上传控件一般为自己封装的,用传统的上传已经不好用了, ...

  4. 【Java例题】5.3 线性表的使用

    3.线性表的使用.使用ArrayList模拟一个一维整数数组.数据由Random类随机产生.进行对输入的一个整数进行顺序查找.并进行冒泡排序. package chapter6; import jav ...

  5. SVG和canvas渲染的性能比较

    1.什么是SVG? 描述: 一种使用XML描述的2D图形的语言 SVG基于XML意味着,SVG DOM中的每个元素都是可用的,可以为某个元素附加Javascript事件处理器. 在 SVG 中,每个被 ...

  6. vscode c 语言 win10

    在看 CSAPP 一些课程,一些c 语言的小程序的例子,想跑起来试试,用一个DEV  c++  简单上手,但这是一个上古的IDE, 前端开发中的代码不全,语法高亮,都不太好,就想着为什么不折腾一下 V ...

  7. DataPipeline丨DataOps的组织架构与挑战

    作者:DataPipeline CEO 陈诚 前两周,我们分别探讨了“数据的资产负债表与现状”及“DataOps理念与设计原则”.接下来,本文会在前两篇文章的基础上继续探讨由DataOps设计原则衍生 ...

  8. 洛谷 P3203 [HNOI2010]弹飞绵羊

    题意简述 有n个点,第i个点有一个ki,表示到达i这个点后可以到i + ki这个点 支持修改ki和询问一点走几次能走出所有点两个操作 题解思路 分块, 对于每个点,维护它走到下一块所经过的点数,它走到 ...

  9. Android 框架揭秘 --读书笔记

    Android 框架揭秘 Insied the Android Framework

  10. 【数学+思维】ZZULIOJ 1531: 小L的区间求和

    题目链接 题目描述 在给定的一个整数序列中,小L希望找到一个连续的区间,这个区间的和能够被k整除,请你帮小L算一下满足条件的最长的区间长度是多少. 输入 第一行输入两个整数n.k.(1 <= n ...