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 ...
随机推荐
- poj2909 欧拉素数筛选
刚刚学了一种新的素数筛选法,效率比原先的要高一些,据说当n趋近于无穷大时这个的时间复杂度趋近O(n).本人水平有限,无法证明. 这是道水题,贴代码出来重点是欧拉筛选法.我把原来普通的筛选法贴出来. / ...
- Dubbo源码学习之-服务导出
前言 忙的时候,会埋怨学习的时间太少,缺少个人的空间,于是会争分夺秒的工作.学习.而一旦繁忙的时候过去,有时间了之后,整个人又会不自觉的陷入一种懒散的状态中,时间也显得不那么重要了,随便就可以浪费掉几 ...
- 1、大型项目的接口自动化实践记录--robotframework环境搭建
因为人力.团队技术问题,选用robotframework来做自动化,首先说下环境搭建 齐涛道长的入门教程非常棒:http://blog.csdn.net/tulituqi/article/detail ...
- 4如何用PHP给MySQL数据库添加记录
首先连接数据库(依旧用第二篇的方法) 假设数据库表里只有id,name,email三列 添加以下代码 $inputemail=写你要的email;$inputname=写你要的name;//先设定你要 ...
- The introduction of the book American daily English notes (enlarged edition)
After reading the book of American daily English notes written by Linkun Yang[1], I think I should a ...
- openjdk:8u22-jre-alpine在java开发中的NullPointerException错误解决方案
问题描述 ** 在SpringBoot项目中使用了Ureport报表组件, 打包发布部署到docker中启动报错 ** java.lang.NullPointerException at sun.aw ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- 学习spark 技术
spark sql 可以说是 spark 中的精华部分了,我感觉整体复杂度是 spark streaming 的 5 倍以上,现在 spark 官方主推 structed streaming, spa ...
- JVM调优之经验
在生产系统中,高吞吐和低延迟一直都是JVM调优的最终目标,但这两者恰恰又是相悖的,鱼和熊掌不可兼得,所以在调优之前要清楚舍谁而取谁.一般计算任务和组件服务会偏向高吞吐,而web展示则偏向低延迟才会带来 ...
- MYSQL 入门配置
1.下载 MYSQL官网 2.目录结构图基本如下 3.运行CMD(管理员权限),进入MYSQL目录下面的bin目录 4.执行 mysqld install 5.执行 net start mysql 6 ...