B. Arpa’s weak amphitheater and Mehrdad’s valuable Hoses
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Just to remind, girls in Arpa’s land are really nice.

Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, …, ak such that ai and ai + 1 are friends for each 1 ≤ i < k, and a1 = x and ak = y.

Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa’s amphitheater can hold at most w weight on it.

Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn’t exceed w.
Input

The first line contains integers n, m and w (1  ≤  n  ≤  1000, , 1 ≤ w ≤ 1000) — the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.

The second line contains n integers w1, w2, …, wn (1 ≤ wi ≤ 1000) — the weights of the Hoses.

The third line contains n integers b1, b2, …, bn (1 ≤ bi ≤ 106) — the beauties of the Hoses.

The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.
Output

Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn’t exceed w.
Examples
Input

3 1 5
3 2 5
2 4 2
1 2

Output

6

Input

4 2 11
2 4 6 6
6 4 2 1
1 2
2 3

Output

7

Note

In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.

In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can’t invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.

有关系的并查集分在一组~01背包~每组选一个或全选~更新最优解即可~~

AC代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node{int x,y;
}st[1010];
int t[1010],dp[1010];
vector <int> v[1010];
int find(int x){while(x != t[x])x = t[x];return x;
}
int main()
{int N,M,W,a,b;scanf("%d %d %d",&N,&M,&W);for(int i = 1 ; i <= N; i++)scanf("%d",&st[i].x);for(int i = 1 ; i <= N; i++){scanf("%d",&st[i].y);t[i] = i;}while(M--){scanf("%d %d",&a,&b);int fa = find(a),fb = find(b);if(fa != fb)t[fa] = fb;}for(int i = 1 ; i <= N; i++)v[find(i)].push_back(i); // 建组for(int i = 1 ; i <= N; i++)if(find(i) == i){ // 一个组遍历一次for(int u = W; u >= 1 ; u--){int fw = 0,fb = 0;for(int j = 0 ; j < v[i].size(); j++){fw += st[v[i][j]].x;fb += st[v[i][j]].y;if(u >= st[v[i][j]].x)dp[u] = max(dp[u],dp[u - st[v[i][j]].x] + st[v[i][j]].y);}if(u >= fw)dp[u] = max(dp[u],dp[u - fw] + fb);}}printf("%d\n",dp[W]);return 0;
}

【Codeforces 741 B. Arpa's weak amphitheater and Mehrdad's 】+ 并查集 + 01背包相关推荐

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

  2. Codeforces 741 D - Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    D - Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 思路: 树上启发式合并 从根节点出发到每个位置的每个字符的奇偶性记为每个位 ...

  3. Codeforces 742B B. Arpa’s obvious problem and Mehrdad’s terrible solution

    B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...

  4. Codeforces Round #548 (Div. 2) C. Edgy Trees(dfs || 并查集)

    题目链接:https://codeforces.com/contest/1139/problem/C 题意:给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路 ...

  5. 【CodeForces】741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

    [题意]给定n个点的树,每条边有一个小写字母a~v,求每棵子树内的最长回文路径,回文路径定义为路径上所有字母存在一种排列为回文串.n<=5*10^5. [算法]dsu on tree [题解]这 ...

  6. 【codeforces 742A】Arpa’s hard exam and Mehrdad’s naive cheat

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. Codeforces 742 C Arpa's loud Owf and Mehrdad's evil plan

    题目地址:http://codeforces.com/contest/742/problem/C 题意:我看了好多别人的博客的对于题意的理解,结合他们的代码我认为这道题的意思应该是每个数要都能经过t步 ...

  8. CF - 741(C. Arpa’s overnight party and Mehrdad’s silent entering) 二分图构造

    题目链接 题意 已经给出情侣的座位编号,将两种饭分配给N对情侣.满足以下两个要求: 情侣之间不能吃相同的饭 座位号连续的三个人不能吃同一种饭 问是否存在满足的解. 思路 构造二分图 对应情侣的座位建边 ...

  9. 【CodeForces - 371D】Vessels(思维,元素合并,并查集)

    题干: There is a system of n vessels arranged one above the other as shown in the figure below. Assume ...

最新文章

  1. 如何用Dart写一个单例
  2. 第十一届河南省赛--山区修路
  3. cstring和string的区别
  4. matlab谐波仿真代码,matlab的谐波仿真程序基于ip-iq法???怎么出不来图像啊???...
  5. [天地君亲若追问 枉为知音百年羞]2008.06.07 晃荡在芳华
  6. 2019王小的Java学习之路
  7. c语言数据交换的算法流程图,C语言冒泡排序算法浅析
  8. 使用JSSDK分享页面
  9. 尚硅谷kubernates学习笔记 1
  10. Excel VBA 函数返回值
  11. 电子元件-继电器知识汇总
  12. Map集合中的Map.Entry的定义:
  13. 32位计算机中内存地址如何表示,内存地址是什么
  14. [架构之路-159]-《软考-系统分析师》-10-系统分析-6-现有业务流程分析, 系统分析最核心的任务
  15. 以太网 DHCP(简介、DHCP工作原理、租期时间)
  16. springBoot使用redis获取自增序号
  17. 如何估算剩余的bug数量
  18. 安卓手机的内置传感器
  19. 3DMAX和MAYA软件上功能上有什么不同
  20. 惊了!身价 220 亿的地产大亨潘石屹要学 Python 了……

热门文章

  1. 高通平台WIFI-去掉信号标识上面的叹号和叉叉issue
  2. 贝省登录时显示服务器异常请稍后再试,网站服务器出现service unavailable错误如何解决?...
  3. Alpha测试与Beta测试及区别
  4. 转:资本2010《CCTV财经频道中国证券市场投资策略报告》发布
  5. Redis教程之基础-五种数据基本操作
  6. benchmark datasets是什么
  7. opencv之基本形状识别
  8. ​​​​​​​ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry ’1′ for key
  9. perp系列之六:perp工作截屏
  10. 汉语编程联姻大中院校