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. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  2. web socket (记录下来方便观看)

    Web Sockets HTML5 WebSocket 设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力. 浏览器通过 JavaScript ...

  3. android系统release签名

    转自:http://blog.csdn.net/yangkai6121/article/details/38682321 为什么需要给Android系统签个名才能进行CTS认证呢?原来我们通过make ...

  4. ***PHP 数组排序 +php二维数组排序方法(PHP比较器)

    PHP - 一维数组的排序函数 在本节中,我们将学习如下 PHP 数组排序函数: sort() - 以升序对数组排序 rsort() - 以降序对数组排序 asort() - 根据值,以升序对关联数组 ...

  5. Android Studio中Button等控件的Text中字符串默认大写的解决方法

    初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...

  6. MachineKey 操作 之 应用集群中SSO应用生成MachineKey

    MachineKey介绍 MachineKey其用于对 Forms 身份验证 Cookie 数据和视图状态数据进行加密和解密,一般情况下IIS自动默认给网站或者每一个应用生成唯一的MachineKey ...

  7. Spring MVC重定向和转发及异常处理

    SpringMVC核心技术---转发和重定向 当处理器对请求处理完毕后,向其他资源进行跳转时,有两种跳转方式:请求转发与重定向.而根据要跳转的资源类型,又可分为两类:跳转到页面与跳转到其他处理器.对于 ...

  8. 【CentOS】LAMP相关3

    调优,安全如果是运维一个网站,PHP搭建的话,可能会出现500的错误,白页怎么去排查呢,今天就涉及到这方面的东西 http://blog.csdn.net/bsi_l4/article/details ...

  9. HTML导航栏

    先看效果(两种,1:自己写样式,写交互,2.用jQueryUI 的menu),如下图 第一种:       第二种:   第一种样式: 然后就开始准备了,单村用js和css也可以写出来,不过既然有jq ...

  10. CC2530使用串口下载(SBL)

    工作环境: WIN7 64位 IAR 版本: 8.10.3 (8.10.3.10338) ZStack-CC2530-2.3.1-1.4.0协议栈,下载地址:http://download.csdn. ...