D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题
http://codeforces.com/problemset/problem/742/D
并查集预处理出所有关系。
一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判断。
这样是不对的。因为这样使得同一组里面可能选择了两次。
3 0 2
1 2 3
1 1 3
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 2e3 + ;
int w[maxn];
int bea[maxn];
int fa[maxn];
int tofind(int x) {
if (fa[x] == x) return x;
else return fa[x] = tofind(fa[x]);
}
void tomerge(int x, int y) {
x = tofind(x);
y = tofind(y);
fa[y] = x;
}
vector<int>a[maxn];
int dp[ + ];
void work() {
int n, m, tot;
cin >> n >> m >> tot;
for (int i = ; i <= n; ++i) fa[i] = i;
for (int i = ; i <= n; ++i) cin >> w[i];
for (int i = ; i <= n; ++i) cin >> bea[i];
for (int i = ; i <= m; ++i) {
int u, v;
cin >> u >> v;
tomerge(u, v);
}
for (int i = ; i <= n; ++i) {
a[tofind(i)].push_back(i);
}
int tn = n;
for (int i = ; i <= n; ++i) {
if (a[i].size() == ) continue;
int ww = , bb = ;
for (int k = ; k < a[i].size(); ++k) {
ww += w[a[i][k]];
bb += bea[a[i][k]];
}
w[++tn] = ww;
bea[tn] = bb;
a[i].push_back(tn);
}
for (int i = ; i <= n; ++i) {
if (a[i].size() == ) continue;
for (int j = tot; j >= ; --j) {
for (int k = ; k < a[i].size(); ++k) {
if (j >= w[a[i][k]]) {
dp[j] = max(dp[j], dp[j - w[a[i][k]]] + bea[a[i][k]]);
}
}
}
// int ww = 0, bbea = 0;
// for (int k = 0; k < a[i].size(); ++k) {
// ww += w[a[i][k]];
// bbea += bea[a[i][k]];
// }
// for (int j = tot; j >= ww; --j) {
// dp[j] = max(dp[j], dp[j - ww] + bbea);
// }
}
cout << dp[tot] << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
- 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 个人是一个 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
<题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses
[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...
随机推荐
- 解决echart在IE中使用时,在div中加入postion后图表不显示问题
<!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="height:400px;width:1 ...
- Windows驱动程序开发基础(四)驱动的编译调试和安装
Windows驱动程序开发基础,转载标明出处:http://blog.csdn.net/ikerpeng/article/details/38793995 以下说一下开发出来驱动程序以后怎样编译.一般 ...
- 常见网络摄像机默认使用的端口,RTSP地址
品牌 默认IP地址 WEB RTSP HTTPS 数据 ONVIF 海康威视 192.168.1.64/DHCP用户名admin 密码自己设 80 554 443 8000 80 大华 192 ...
- c语言学习-指针探究
1:指针定义格式:格式:变量类型 *变量名用途:指针变量用于储存地址(only),也就是根据地址值,访问对应的存储空间. 注意.int *p 只能指向int类型的数据: 例: int a = 20; ...
- 为Windows Live Writer添加Code插件
1.插件效果展示 1: void CDemoDlg::OnBnClickedNmdlg() 2: { 3: CNonModeDlg *pDlg = new CNonModeDlg();// 创建一个C ...
- 返回模式有流式(streaming)和整体(total) 热词词表解决方案
重要术语说明_语音识别(ASR)_智能语音交互-阿里云 https://help.aliyun.com/document_detail/72238.html 返回模式(response mode) ...
- 修改系统时间(取得服务器时间,使用SetLocalTime API函数,需要UAC权限)
我的客户遇到系统时间不对,自己又不会改,于是想到利用服务端时间来修改本地的系统时间. 第一步,把下面xml存成uac.xml文件备用. <?xml version="1.0" ...
- NS3网络仿真(4): DataRate属性
快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在first.py中创建了一个点到点的信道,且配置了两个属性: pointToPoint = ns ...
- SAP事务码 一
SE80 -- edit source code. SE24 -- class create or display. SFP -- created and maintained independent ...
- vue 子组件 索引 ref
尽管有props和events,但是有时任然需要在javascript中直接访问子组件.为此可是使用ref为子组件指定一个索引ID. <div id="parent"> ...