【题目链接】 http://codeforces.com/problemset/problem/741/B

【题目大意】

  给出一张图,所有连通块构成分组,每个点有价值和代价,
  要么选择整个连通块,要么只能在连通块中选择一个,或者不选,为最大价值

【题解】

  首先我们用并查集求出连通块,然后对连通块进行分组背包即可。

【代码】

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#define rep(i,n) for(int i=1;i<=n;i++)
using namespace std;
const int N=1010;
int dp[N],f[N],n,m,x,y,size,w[N],b[N];
vector<int> v[N];
int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
int main(){
while(~scanf("%d%d%d",&n,&m,&size)){
rep(i,n)f[i]=i,v[i].clear();
rep(i,n)scanf("%d\n",&w[i]);
rep(i,n)scanf("%d\n",&b[i]);
rep(i,m){scanf("%d%d",&x,&y);f[sf(x)]=sf(y);}
rep(i,n)v[sf(i)].push_back(i);
memset(dp,0,sizeof(dp));
rep(i,n)if(sf(i)==i){
for(int j=size;j>=0;j--){
int W=0,B=0;
for(int k=0;k<v[i].size();k++){
W+=w[v[i][k]]; B+=b[v[i][k]];
if(j>=w[v[i][k]])dp[j]=max(dp[j],dp[j-w[v[i][k]]]+b[v[i][k]]);
}if(j>=W)dp[j]=max(dp[j],dp[j-W]+B);
}
}printf("%d\n",dp[size]);
}return 0;
}

Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses的更多相关文章

  1. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)

    <题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...

  2. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

  3. Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)

    题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...

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

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

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

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

  8. Codeforces 741B:Arpa's weak amphitheater and Mehrdad's valuable Hoses(01背包+并查集)

    http://codeforces.com/contest/741/problem/B 题意:有 n 个人,每个人有一个花费 w[i] 和价值 b[i],给出 m 条边,代表第 i 和 j 个人是一个 ...

  9. 【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. 在ubuntu16.04+python3.5情况下安装nltk,以及gensim时pip3安装不成功的解决办法

    在ubuntu16.04+python3.5情况下安装nltk,以及gensim时pip3安装不成功的解决办法,我刚开始因为不太会用linux命令,所以一直依赖于python 的pip命令,可是怎么都 ...

  2. js删除数组中重复的元素

    1.方法一 将数组逐个搬到另一个数组中,当遇到重复元素时,不移动,若元素不重复则移动到新数组中 function unique(arr){ var len = arr.length; var resu ...

  3. sublime在搜索的时候排除js文件

    代码审计的时候sublime是一个神器.所以.... Ctrl + Shift + F /home/i3ekr/Desktop/coding/phpcms,*.php 这样就可以直接搜索所有的php文 ...

  4. python中的ftplib模块

    前言 Python中默认安装的ftplib模块定义了FTP类. ftplib模块相关参数: 加载ftp模块:from ftplib import FTP ftp = FTP()#设置变量ftp.set ...

  5. uboot makefile构建分析

    前言 几年前分析过uboot的构建及启动过程,做了笔记,但最终没有转为文章.这次又有机会开发嵌入式产品了(之前一年多都是在搞x86 linux),看了下uboot的构建过程,觉得有必要写下整个分析过程 ...

  6. linux的基本的命令行操作

    linux的基本的命令行操作 第一步前登陆你的服务器 //创建文件夹的方法 mkdir 文件名 //进入指定文件夹 cd 文件名 //查看文件夹下的内容 ls or ll // 查看当前的路径 pwd ...

  7. PHP配置Configure报错:Please reinstall the libzip distribution

    PHP配置Configure报错:Please reinstall the libzip distribution 发生情景: php执行配置命令configure时,报如下错误: checking ...

  8. leetcode 之Plus One(9)

    这题需要注意的是最后的进位 vector<int> plusOne(vector<int>& nums,int num) { add(nums, num); } voi ...

  9. 玩转RaspberryPi

    step1:烧制树莓派内存卡 可以用[Linux系统烧制]http://www.williamsang.com/archives/1764.html 如果用windows烧制的话,就用Win32 Di ...

  10. Matlab处理数据导出Paraview可读的vtk文件(一)

    Paraview是一个开源的可视化软件. 用到matlab子程序从这里下载 或者到博客末尾复制粘贴 子程序名为 vtkwrite 示例1: load mri D = squeeze(D); vtkwr ...