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 ...
随机推荐
- ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core
前言 原本本节内容是不存在的,出于有几个人问到了我:我想使用ASP.NET Core Identity,但是我又不想使用默认生成的数据库表,想自定义一套,我想要使用ASP.NE Core Identi ...
- HDP Hive性能调优
(官方文档翻译整理及总结) 一.优化数据仓库 ① Hive LLAP 是一项接近实时结果查询的技术,可用于BI工具以及网络看板的应用,能够将数据仓库的查询时间缩短到15秒之内,这样的查询称之为Int ...
- lvs模式及算法
一.三种模式 (一).Virtual Servervia Network Address Translation(VS/NAT) 通过网路地址转换,调度器重写请求报文的目标地址,根据预设的调度算法,将 ...
- 《深入理解Java虚拟机》- Java虚拟机是如何加载Java类的?
Java虚拟机是如何加载Java类的? 这个问题也就是面试常问到的Java类加载机制.在年初面试百战之后,菜鸟喜鹊也是能把这流程倒背如流啊!但是,也只是字面上的背诵,根本就是像上学时背书考试一样. ...
- JS实现循环删除数组中元素的方法介绍
这篇文章主要给大家介绍了关于Javascript循环删除数组中元素的几种方法,文中给出了详细的示例代码供大家参考学习,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧. 本文主要跟大家分享了 ...
- Delegate,Block,Notification, KVC,KVO,Target-Action
Target-Action: 目标-动作机制,所有的UIControl及子类都是这个机制:原理:在对象产生某个事件的特定时刻,给一个对象发送一个消息:类内部target去执行action方法 Dele ...
- sql server中的cte
从SQL Server 2005开始,提供了CTE(Common Table Expression,公用表表达式)的语法支持. CTE是定义在SELECT.INSERT.UPDATE或DELETE语句 ...
- vsftpd 530 Login incorrect问题处理
vsftpd 530 login incorrect 的N中情况 1.密码错误. 2.检查/etc/vsftpd/vsftpd.conf配置 vim /etc/vsftpd/vsftpd.conf 看 ...
- Scala 系列(八)—— 类和对象
一.初识类和对象 Scala 的类与 Java 的类具有非常多的相似性,示例如下: // 1. 在 scala 中,类不需要用 public 声明,所有的类都具有公共的可见性 class Person ...
- 以太坊智能合约[ERC20]发币记录
以太坊被称为区块链2.0,就是因为以太坊在应用层提供了虚拟机,使得开发者可以基于它自定义逻辑,通常被称为智能合约,合约中的公共接口可以作为区块链中的普通交易执行.本文就智能合约发代币流程作一完整介绍( ...