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 nm 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 ≤ nxi ≠ yi), meaning that Hoses xiand 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.

分析:分组背包;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=(int)m;i<=(int)n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,w,v[maxn],b[maxn],now;
bool vis[maxn];
ll dp[][maxn],v1,b1;
vi e[maxn];
void dfs(int p)
{
vis[p]=true;
v1+=v[p],b1+=b[p];
for(int i=w;i>=v[p];i--)dp[now][i]=max(dp[now][i],dp[now^][i-v[p]]+b[p]);
for(int x:e[p])
{
if(!vis[x])
{
dfs(x);
}
}
}
int main()
{
int i,j;
scanf("%d%d%d",&n,&m,&w);
rep(i,,n)scanf("%d",&v[i]);
rep(i,,n)scanf("%d",&b[i]);
rep(i,,m)scanf("%d%d",&j,&k),e[j].pb(k),e[k].pb(j);
rep(i,,n)
{
if(!vis[i])
{
now^=;
v1=b1=;
rep(j,,w)dp[now][j]=dp[now^][j];
dfs(i);
for(j=w;j>=v1;j--)dp[now][j]=max(dp[now][j],dp[now^][j-v1]+b1);
}
}
printf("%lld\n",dp[now][w]);
//system("Pause");
return ;
}

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

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

  4. 【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 ...

  5. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

  6. 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 个人是一个 ...

  7. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)

    <题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...

  8. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

    [题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么 ...

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

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

随机推荐

  1. AS代码优化和Flex应用程序的性能

    1.在局部变量够用时,不要使用全局变量.类静态变量也要少用.全局变量是开发者的恶梦.实在需要全局变量的话,我建议使用singleton设 计模式来进行管理. 2.读取数组中的数据,尽量使用for in ...

  2. CentOS 安装 Mogodb(在线 && 离线)

    平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html 先说推荐安装:在线安装,跟着官方走可以避免一些问题 新学Mon ...

  3. Android中使用开源框架Fresco处理图片

    本文为原创博文,转载请注明原文链接:http://www.cnblogs.com/panhouye/p/6278116.html 关于Fresco的优点大家自行谷歌吧,它太强大太优秀了,我这一片小博文 ...

  4. CSS倒影

    前面的话   CSS倒影目前只有chrome和safari浏览器支持,且需要添加-webkit-前缀.本文将详细介绍CSS倒影box-reflect 语法 -webkit-box-reflect 初始 ...

  5. Windows 8 卡在正在检查更新

    原文地址:https://answers.microsoft.com/en-us/windows/forum/windows_8-update/windows-update-not-updating- ...

  6. AR/VR行业是否会像智能手机一样的飞速崛起

    从硬件和内容两方面来看,VR(虚拟现实)应该会比AR(增强现实)率先普及大众.当然,这是建立在解决无线化.眩晕.便携等问题之后的事儿,内容上不用担心,照现在这个发展速度-- 说到"风口&qu ...

  7. JAVA-代理学习一之JDK实现

    代理的实现依赖于反射,建议不太懂反射的童鞋先看看反射相关的知识点. 代理可以理解为对实际调用方法的一种能力的加强. 代理分为静态代理和动态代理: <1> 静态代理示例 接口MyInterf ...

  8. python新手 实践操作 作业

    #有如下值集合 [11,22,33,44,55,66,77,88,99],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中.即: {'k1': 大于66的所 ...

  9. java--调整JVM内存的大小

    默认占用:64M的内存 修改内存的方式: 1.某一类,右键选择--Run Configurations 2.选择--Arguments 3.在VM argments中输入内容,如:-Xmx80m

  10. Freeplane中的自动边线颜色功能

    今天我将电脑上的Freeplane从1.3.11升级到了1.5.18.发现新版本已经没有了1.3.11中的菜单选项Format → “Automatic edge color”.搜索了一下才发现,该功 ...