http://codeforces.com/contest/742/problem/D

题目大意:有n个人,每个人有重量wi和魅力值bi。然后又有m对朋友关系,朋友关系是传递的,如果a和b是朋友,b和c是朋友,那么a和c就是朋友。现在,把所有能作为朋友的人放在一个集合里面。你现在要开一个party,这个party的容量为W,现在,你每次只能选择一个集合里面的一个人或者选择集合里面的所有人进入这个party。在满足总w <= W的情况下,总魅力值b最大,问魅力值最大是多少?

思路:

定义dp(i, j)表示目前是第i个集合,party里面的重量为j的最大魅力值。

dp(i, j) = max(dp[i - 1][j], dp[i-1][j - 集合的总w(或集合的某个w)]) + 集合的总b(或集合中的某一个b)

复杂度O(n*n)

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int dp[maxn][maxn];
vector<pair<int, int> > subset[maxn];
pair<int, int> f[maxn];
int par[maxn];
int n, m, w; int pfind(int x){
if (par[x] == x) return x;
return par[x] = pfind(par[x]);
} map<int, int> id;
int cnt = ;
int get_id(int x){
if (id.count(x) == ) id[x] = ++cnt;
return id[x];
} int main(){
cin >> n >> m >> w;
for (int i = ; i <= n; i++) par[i] = i;
for (int i = ; i <= n; i++)
scanf("%d", &f[i].fi);
for (int i = ; i <= n; i++)
scanf("%d", &f[i].se);
for (int i = ; i <= m; i++){
int u, v; scanf("%d%d", &u, &v);
int pu = pfind(u), pv = pfind(v);
par[pv] = pu;
} for (int i = ; i <= n; i++){
int myid = get_id(pfind(i));
subset[myid].pb(f[i]);
} for (int i = ; i <= cnt; i++){
for (int j = w; j >= ; j--){
int totw = , totb = ;
dp[i][j] = dp[i - ][j];
for (int k = ; k < subset[i].size(); k++){
pair<int, int> p = subset[i][k];
totw += p.fi, totb += p.se;
if (p.fi > j) continue;
dp[i][j] = max(dp[i][j], dp[i - ][j - p.fi] + p.se);
}
if (totw <= j){
dp[i][j] = max(dp[i][j], dp[i - ][j - totw] + totb);
}
}
} int ans = ;
for (int i = ; i <= w; i++)
ans = max(ans, dp[cnt][i]);
printf("%d\n", ans);
return ;
}

01背包dp+并查集 Codeforces Round #383 (Div. 2)的更多相关文章

  1. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  2. 贪心+bfs 或者 并查集 Codeforces Round #268 (Div. 2) D

    http://codeforces.com/contest/469/problem/D 题目大意: 给你一个长度为n数组,给你两个集合A.B,再给你两个数字a和b.A集合中的每一个数字x都也能在a集合 ...

  3. DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...

  4. Codeforces Round #383 (Div. 2) 题解【ABCDE】

    Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...

  5. Codeforces Round #383 (Div. 2)D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(dp背包+并查集)

    题目链接 :http://codeforces.com/contest/742/problem/D 题意:给你n个女人的信息重量w和美丽度b,再给你m个关系,要求邀请的女人总重量不超过w 而且如果邀请 ...

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

  7. Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包

    A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...

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

  9. Codeforces Round #383 (Div. 1)

    A: 题目大意:给出一个有向图(n<=100),每个点的出度都为1,求最小的t,使得任意两点x,y,如果x走t步后能到y,那么y走t步后到x. 题解: 首先每个点应该都在一个环上,否则无解. 对 ...

随机推荐

  1. 初始化git

    git config --global user.name "Firstname Lastname" git config --global user.email "yo ...

  2. 安装器---Inno Setup

    Inno Setup[1]  用Delphi写成,其官方网站同时也提供源程序免费下载.它虽不能与Installshield这类恐龙级的安装制作软件相比,但也当之无愧算是后起之秀.Inno Setup是 ...

  3. 基于PHP——简单的WSDL的创建(WSDL篇)

    1.建立WSDL文件      建立WSDL的工具很多,eclipse.zendstudio.vs都可以,我个人建议自己写,熟悉结构,另外自动工具对xml schame类型支持在类型中可能会报错. 下 ...

  4. @Autowired注解(转)

    5.6.4 @Autowired注解 自Spring诞生以来,

  5. Linux下Modules的概念及使用详解[转贴]

    一.什么是 modules? modules 的字面意思就是模块,在此指的是 kernel modules:简单来说,一个模块提供了一个功能,如 isofs.minix.nfs.lp 等等.传统来讲, ...

  6. JavaScript——Cookie

    JavaScript中的Cookie基础 页面用来保存信息的,比如登录.记住用户名. [cookie的特性] (1)同一个网站中所有页面共享一套cookie: (2)数量.大小有限: (3)有保质期, ...

  7. SQLAlchemy入门

    什么是SQLAlchemy? 是Python连接SQL数据库的一种方式,需要通过驱动来连接. 是Python中使用最广泛的ORM(对象关系映射)工具之一,即把数据库中的二维表结构映射成一个list对象 ...

  8. npm 使用代理

    npm install 有时候会安装失败,可能是网络的问题,可以使用代理来安装 npm获取配置有6种方式,优先级由高到底. 命令行参数. --proxy http://server:port即将pro ...

  9. 手机浏览器无法获取COOKIE的原因

    手机浏览器上无法使用cookie,肯能是 1. 浏览器禁用 COOKIE ,这个简单开启即可. 2. 可能是手机所在时区有问题,将COOKIE有效期设置更长时间测试下,在更改时区

  10. C#_FindWindow

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...