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. SQL yog过期后教你怎么让他不过期

    打开注册表  安装sqlyog后,进入注册表编辑器,进入\HEYK_CURRENT_USER\Software\,找到以{}括起来的那项直接干掉! 1, regedit 2,修改 3,

  2. MySQL存储结构的使用

    前言 今天公司老大让我做一个MySQL的调研工作,是关于MySQL的存储结构的使用.这里我会通过3个样例来介绍一下MySQL中存储结构的使用过程,以及一些须要注意的点. 笔者环境 系统:Windows ...

  3. convnet源代码解析(一):基础准备

    Jeremy Lin ConvNet是一个基于GPU实现的卷积神经网络开源码(C++11).是由多伦多大学的Geoffrey Hinton深度学习团队编写的,它的最初版本号是Hinton的学生Alex ...

  4. OpenCV基本图像容器Mat的几种创建方法

    參考文章:http://www.cnblogs.com/tornadomeet/archive/2012/07/19/2599376.html 实验说明: (引用) 本文主要讲一些opencv 2.0 ...

  5. js可视区域图片懒加载

    可视区域图片懒加载 实现原理,页面滚动时获取需要懒加载的图片,判断此图片是否在可视区域内,是则设置图片data-src地址为src地址,加载图片. html下载地址 <!DOCTYPE html ...

  6. Flex+Java+Blazeds

    1.环境:jdk1.6,Flex4.6 2.工具:MyEclipse10 3.server:Tomcat7 4.连接方式:Blazeds 5.项目类型:Flex项目 6.步骤 (1)新建Flex项目一 ...

  7. oracle 12c 13姨

    搞了一下oracle 12c.有些体会还是先记下来. 12c搞搞新意思,弄了个CDB(容器数据库,可不是商务中心CBD哟)和PDB(可插拔数据库).PDB插在CDB里. 简单而言,CDB就是一个数据库 ...

  8. 命令行方式下登录SqlPlus,密码含特殊字符

    全撞上了! 真难侍候!oracle 12c,想登录sql plus,结果没有图形界面,直接出来个命令行.这下好了,我这个数据库,多实例,意味着登录要指定实例:密码中含有特殊字符"@" ...

  9. Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content

    Content-Type https://tools.ietf.org/html/rfc7231#section-3.1.1.5 https://tools.ietf.org/html/rfc7231 ...

  10. js权威指南

    有很多js的细节基础不太了解,希望能通过阅读这本书查漏补缺!长期更新!