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

题意:

有n个女孩,每个女孩有重量和美丽度,有m对朋友关系,朋友关系可传递。邀请他们参加聚会,每一个朋友圈的人要么全部参加,要么不参加,要么只有一个人参加。问在不超过总重量w的条件下最大美丽度是多少。

代码:

 //显然背包,分两种物品,单个的人,整个集合的人。
#include<bits\stdc++.h>
using namespace std;
long long dp[];
int hm[],hd[],vis[],fat[];
int n,m,w;
vector<int>L[];
int find(int x)
{
if(fat[x]!=x)
fat[x]=find(fat[x]);
return fat[x];
}
void connect(int x,int y)
{
int xx=find(x),yy=find(y);
if(xx!=yy)
fat[yy]=xx;
}
void init()
{
for(int i=;i<=n;i++)
fat[i]=i;
}
int main()
{
scanf("%d%d%d",&n,&m,&w);
for(int i=;i<=n;i++)
scanf("%d",&hm[i]);
for(int i=;i<=n;i++)
scanf("%d",&hd[i]);
int x,y;
init();
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
connect(x,y);
}
memset(vis,,sizeof(vis));
int cnt=;
for(int i=;i<=n;i++)
{
int tem=find(i);
if(!vis[tem])
{
L[++cnt].push_back(tem);
if(tem!=i)
L[cnt].push_back(i);
vis[tem]=cnt;
}
else if(vis[tem]&&tem!=i)
{
L[vis[tem]].push_back(i);
}
}
memset(dp,,sizeof(dp));
for(int i=;i<=cnt;i++)
{
int len=L[i].size();
long long sum1=,sum2=;
for(int j=;j<len;j++)
{
sum1+=hm[L[i][j]];
sum2+=hd[L[i][j]];
}
for(int k=w;k>=;k--)
{
if(k>=sum1) //整个集合的
dp[k]=max(dp[k],dp[k-sum1]+sum2);
for(int j=;j<len;j++) //单个的
{
int tem=L[i][j];
if(k>=hm[tem])
dp[k]=max(dp[k],dp[k-hm[tem]]+hd[tem]);
}
}
}
cout<<dp[w]<<endl;
return ;
}

CF2.D 并查集+背包的更多相关文章

  1. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  2. POJ - 1417 并查集+背包

    思路:很简单的种类并查集,利用并查集可以将所有的人分成几个集合,每个集合又分为好人和坏人集合,直接进行背包dp判断有多少种方法可以在取了所有集合并且人数正好凑足p1个好人的方案.dp(i, j)表示前 ...

  3. poj1417(带权并查集+背包DP+路径回溯)

    题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...

  4. 并查集+背包 【CF741B】 Arpa's weak amphitheater and Mehrdad's valuable Hoses

    Descirption 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证 ...

  5. poj1417 True Liars[并查集+背包]

    有一点小转化的题,在设计dp状态时还是有点费脑筋的. 地址. 依题意,首先可以知道肯定要扩展域的并查集(明摆着的嘛).一个"好人"域,一个"坏人"域,每句话分两 ...

  6. Luogu P2170选学霸【并查集+背包】By cellur925

    题目传送门 开始看到本题完全认为就是个彻头彻尾的并查集,只要把实力相当的人都并到一个集合中,最后再找一共有多少联通块即可. 后来发现这是大错特错的qwq.因为选了一个集合中的某人,那这个集合中所有人就 ...

  7. 西安邀请赛-D(带权并查集+背包)

    题目链接:https://nanti.jisuanke.com/t/39271 题意:给定n个物品,m组限制,每个物品有个伤害值,现在让两个人取完所有物品,要使得两个人取得物品伤害值之和最接近,输出伤 ...

  8. poj 1417 True Liars(并查集+背包dp)

    题目链接:http://poj.org/problem?id=1417 题意:就是给出n个问题有p1个好人,p2个坏人,问x,y是否是同类人,坏人只会说谎话,好人只会说实话. 最后问能否得出全部的好人 ...

  9. 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 而且如果邀请 ...

随机推荐

  1. 最终版的Web(Python实现)

    天啦,要考试了,要期末考试了,今天把最终版的Python搭建Web代码先写这里记下了.详细的过程先不写了. 这次是在前面的基础上重写 HTTPServer 与 BaseHTTPRequestHandl ...

  2. WPF 如何引入外部样式

    当我们给一些控件设置相同的属性的时候,这时候,我们可以把这些属性写到一个Style里面. 而其他页面也有类似的控件也需要使用这个Style,这时候就需要把这个Style放在一个共通的文件里,然后引入这 ...

  3. iOS开发者联系 联系方式

    苹果开发者客服电话地址:https://developer.apple.com/contact/phone.php 中国大陆地区客服电话: 中国香港地区客服电话:() 中国台湾地区客服电话: 链接地址 ...

  4. 【Java EE 学习 67 下】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】

    一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar ...

  5. ppmoney 总结二

    1. return false   ES6函数的扩展:箭头函数  数组 arr.map()   arr.filter() <!DOCTYPE html> <html lang=&qu ...

  6. JDK7和JDK8一些重要新特性

    jdk7新特性(部分) switch支持字符串 List AutoCloseable接口实现自动关闭,在try()中 新增获取环境信息的工具方法,getJavaHomeDir,getUserHomeD ...

  7. Jetpack 由 WordPress.com 出品

    官网:https://jetpack.com/ Jetpack 由 WordPress.com 出品. Jetpack 通过为您提供访客统计数据和安全服务.加速图像传输以及帮您获得更多浏览量,可以简化 ...

  8. jquery lazyload延迟加载技术的实现原理分析

    懒加载技术(简称lazyload)并不是新技术,它是js程序员对网页性能优化的一种方案.lazyload的核心是按需加载.在大型网站中都有lazyload的身影,例如谷歌的图片搜索页,迅雷首页,淘宝网 ...

  9. VS2013如何避开安装时IE10的限制

    [VS2013]如何避开VS2013必须要有IE10的限制 VS就会告诉我们目前环境不适合安装VS2013,必须升级IE版本到IE10. 在不安装IE10时的时候,安装办法: 将下面一段文字,储存为. ...

  10. 在Spring中轻松写日志

    最近觉得写的一点代码(JAVA),还觉得颇为自得,贡献出来供大家参考. 首先,先上代码: @Controller public class Controller1{ @WriteLog(value = ...