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 分组背包模板题的更多相关文章

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

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

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

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

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

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

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

  7. 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 个人是一个 ...

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

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

  9. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

    [题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...

随机推荐

  1. unix时间戳(unix timestamp)与北京时间的互转方法

    1.在linux bash下北京时间与unix时间戳互转: 获取unix timestamp: 命令:date "+%s" 输出:1372654714 获取北京时间: 命令:dat ...

  2. Rust 1.7.0 语法基础 sep_token 和 non_special_token

    一.分隔符 sep_token 指的是分隔符, 是除了 * 和 + 之外的不论什么符号,通常情况下是使用 , 逗号. 比如: 宏的多个參数分隔,以下代码中的逗号就是 sep_token (target ...

  3. uva live 4394 String painter 区间dp

    // uva live 4394 String painter // // 这一题是训练指南上dp专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己 ...

  4. 跟面试官讲Binder(零)

    面试的时候,面试官问你说,简单说一下Android的Binder机制,你会怎么回答? 我想,我会这么说. 在Android启动的时候,Zygote进程孵化出第一个子进程叫SystemServer,而在 ...

  5. 发挥bat的作用

    from 转自:http://blog.csdn.net/hitlion2008/article/details/7467252 1.什么是Windows BATCH BATCH也就是批处理文件,有时 ...

  6. [iOS]经常使用正則表達式

    经常使用正則表達式大全!(比如:匹配中文.匹配html) 匹配中文字符的正則表達式: [u4e00-u9fa5]    评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包含汉字 ...

  7. JSON: Circular Dependency Errors

    If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directiona ...

  8. YTU 1008: 童年生活二三事

    1008: 童年生活二三事 时间限制: 1000 Sec  内存限制: 64 MB 提交: 842  解决: 592 题目描述 Redraiment小时候走路喜欢蹦蹦跳跳,他最喜欢在楼梯上跳来跳去. ...

  9. Ubuntu18开启redis服务自启动

    设置redis服务开机自启动. 1.创建配置文件夹 sudo mkdir /etc/redis sudo cp /usr/local/redis/redis.conf /etc/redis sudo ...

  10. I.MX6 逻辑分析仪 UART

    /*********************************************************************** * I.MX6 逻辑分析仪 UART * 说明: * ...