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. centos中w使用smbclient连接window出现:session setup failed: NT_STATUS_LOGON_FAILURE

    1. 在window中网络->我自己的电脑->能够查看到共享文件,说明window的共享是正常了; 2. 在window中配置共享时,使用的是仅仅同意超级管理员訪问,可是我把超级管理员改名 ...

  2. Deepin-添加path

    以管理员权限添加path(Debian系列) sudo gedit /etc/profile 添加path路径格式是: export PATH=”$PATH:your path1:your path2 ...

  3. jni——如何转换有符号与无符号数

    java数据结构默认均为有符号数,而通过jni转换到c/c++层,却不一定是有符号数. 如若在java中存储的即为无符号数,则在jni中可将jbyte直接进行类型转换. 若进行操作,则可在计算时,先将 ...

  4. 区分Integer.getInteger和Integer.valueOf、Integer.parseInt() 的使用方法

    Integer类有两个看起来很类似的静态方法,一个是Integer.getInteger(String),另外一个是Integer.valueOf(String).如果只看方法名称的话,很容易将这两个 ...

  5. CV_HAAR_FEATURE_DESC_MAX和CV_HAAR_FEATURE_MAX

    #define CV_HAAR_FEATURE_MAX 3 //提前定义的一个宏,在程序中表示一个haar特征由至多三个矩形组成 #define CV_HAAR_FEATURE_DESC_MAX 20 ...

  6. POJ1195 Mobile phones 【二维树状数组】

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14288   Accepted: 6642 De ...

  7. C语言将10进制转为2进制

    第一种方法: #include<stdio.h> void dectobin(int n); int main() { int x=0; scanf("%d",& ...

  8. TinyXml 与 Rapidxml效率对照

    曾经在做开发中一直使用TinyXml,在网上搜索说Rapidxml的效率比tinyXml高.个人比較喜欢追求效率.所以忍不住尝试性使用Rapidxml. RapidXml 的官方站点例如以下: htt ...

  9. Elasticsearch - 搜索类型与搜索位置

    一.搜索类型: Elasticsearch同意用户选择其所希望的处理查询的方式. 由于存在一些不同的情形,对其使用不同的搜索类型才是合适的.为了控制查询的运行方式,我们能够在请求中使用search_t ...

  10. ora-12541无监听的一种场景

    项目上突然出现无法连接Oracle数据库的情况,提示无监听程序. 现象: 查看 listener.ora配置无问题,用Net Configuration Assistant重建监听,NCA也处于假死状 ...