题目链接 :http://codeforces.com/contest/742/problem/D

题意:给你n个女人的信息重量w和美丽度b,再给你m个关系,要求邀请的女人总重量不超过w

而且如果邀请了一个女人要么就邀请她一个,要么要邀请她还有她所有的朋友。

很明显是一道并查集+背包的问题,并不难。要注意的是背包的写法,由于选择情况有两种

1)只选一个女人

2)选和这个女人有关系的一群女人

于是背包最外层是关系数,即联通块的个数,次外层是背包大小,内层是联通个数(由于选择的要求在一个联通块中

只能选择一个或者全选所以背包大小要放在联通个数外面,毕竟只要选一次)然后就没然后了

  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. const int M = 1010;
  7. const int inf = 0X3f3f3f3f;
  8. int wi[M] , be[M];
  9. vector<int> vc , fr[M];
  10. long long dp[M];
  11. int temp , counts;
  12. int fa[M] , n , m , w;
  13. void init() {
  14. for(int i = 0 ; i <= n ; i++) {
  15. fa[i] = i;
  16. }
  17. }
  18. int getf(int x) {
  19. if(x != fa[x])
  20. fa[x] = getf(fa[x]);
  21. return fa[x];
  22. }
  23. void Union(int x , int y) {
  24. int xx = getf(x);
  25. int yy = getf(y);
  26. if(xx != yy) {
  27. fa[xx] = yy;
  28. }
  29. }
  30. void dfs(int pos) {
  31. for(int i = 1 ; i <= n ; i++) {
  32. int father = getf(i);
  33. if(father == pos) {
  34. fr[temp].push_back(i);
  35. }
  36. }
  37. }
  38. int main() {
  39. cin >> n >> m >> w;
  40. init();
  41. for(int i = 1 ; i <= n ; i++) {
  42. cin >> wi[i];
  43. }
  44. for(int i = 1 ; i <= n ; i++) {
  45. cin >> be[i];
  46. }
  47. for(int i = 1 ; i <= m ; i++) {
  48. int x , y;
  49. cin >> x >> y;
  50. Union(x , y);
  51. }
  52. memset(dp , 0 , sizeof(dp));
  53. temp = 0;
  54. for(int i = 1 ; i <= n ; i++) {
  55. if(fa[i] == i) {
  56. vc.push_back(i);
  57. }
  58. }
  59. int L = vc.size();
  60. for(int i = 0 ; i < L ; i++) {
  61. temp++;
  62. dfs(vc[i]);
  63. }
  64. for(int i = 1 ; i <= temp ; i++) {
  65. int len = fr[i].size();
  66. int sum1 = 0 , sum2 = 0;
  67. for(int j = 0 ; j < len ; j++) {
  68. int p = fr[i][j];
  69. sum1 += wi[p];
  70. sum2 += be[p];
  71. }
  72. for(int l = M - 2 ; l >= 0 ; l--) {
  73. if(l + sum1 < M) {
  74. dp[l + sum1] = max(dp[l + sum1] , dp[l] + sum2);
  75. }
  76. for(int j = 0 ; j < len ; j++) {
  77. int p = fr[i][j];
  78. if(l + wi[p] < M) {
  79. dp[l + wi[p]] = max(dp[l + wi[p]] , dp[l] + be[p]);
  80. }
  81. }
  82. }
  83. }
  84. long long MAX = 0;
  85. for(int i = 0 ; i <= w ; i++) {
  86. MAX = max(MAX , dp[i]);
  87. }
  88. cout << MAX << endl;
  89. return 0;
  90. }

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. ASP.NET Core Identity自定义数据库结构和完全使用Dapper而非EntityFramework Core

    前言 原本本节内容是不存在的,出于有几个人问到了我:我想使用ASP.NET Core Identity,但是我又不想使用默认生成的数据库表,想自定义一套,我想要使用ASP.NE Core Identi ...

  2. HDP Hive性能调优

    (官方文档翻译整理及总结) 一.优化数据仓库 ① Hive LLAP  是一项接近实时结果查询的技术,可用于BI工具以及网络看板的应用,能够将数据仓库的查询时间缩短到15秒之内,这样的查询称之为Int ...

  3. lvs模式及算法

    一.三种模式 (一).Virtual Servervia Network Address Translation(VS/NAT) 通过网路地址转换,调度器重写请求报文的目标地址,根据预设的调度算法,将 ...

  4. 《深入理解Java虚拟机》- Java虚拟机是如何加载Java类的?

    Java虚拟机是如何加载Java类的?  这个问题也就是面试常问到的Java类加载机制.在年初面试百战之后,菜鸟喜鹊也是能把这流程倒背如流啊!但是,也只是字面上的背诵,根本就是像上学时背书考试一样. ...

  5. JS实现循环删除数组中元素的方法介绍

    这篇文章主要给大家介绍了关于Javascript循环删除数组中元素的几种方法,文中给出了详细的示例代码供大家参考学习,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧. 本文主要跟大家分享了 ...

  6. Delegate,Block,Notification, KVC,KVO,Target-Action

    Target-Action: 目标-动作机制,所有的UIControl及子类都是这个机制:原理:在对象产生某个事件的特定时刻,给一个对象发送一个消息:类内部target去执行action方法 Dele ...

  7. sql server中的cte

    从SQL Server 2005开始,提供了CTE(Common Table Expression,公用表表达式)的语法支持. CTE是定义在SELECT.INSERT.UPDATE或DELETE语句 ...

  8. vsftpd 530 Login incorrect问题处理

    vsftpd 530 login incorrect 的N中情况 1.密码错误. 2.检查/etc/vsftpd/vsftpd.conf配置 vim /etc/vsftpd/vsftpd.conf 看 ...

  9. Scala 系列(八)—— 类和对象

    一.初识类和对象 Scala 的类与 Java 的类具有非常多的相似性,示例如下: // 1. 在 scala 中,类不需要用 public 声明,所有的类都具有公共的可见性 class Person ...

  10. 以太坊智能合约[ERC20]发币记录

    以太坊被称为区块链2.0,就是因为以太坊在应用层提供了虚拟机,使得开发者可以基于它自定义逻辑,通常被称为智能合约,合约中的公共接口可以作为区块链中的普通交易执行.本文就智能合约发代币流程作一完整介绍( ...