Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D

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.

Example

Input

3 1 53 2 52 4 21 2

Output

6

Input

4 2 112 4 6 66 4 2 11 22 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.

1.并查集管理集合。

2.把集合看成一件物品,因为要么从集合中取一个元素,要么取整个集合,那么就对这个集合中的每件物品背一次,然后对这件物品背一次。最后背完所有的物品就是答案。。。

易懂的的代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<vector>
 5 using namespace std;
 6
 7 const int INF=0x3f3f3f3f;
 8 const int N=100010;
 9
10 int dp[1010];
11 int fa[1010],c[1010],w[1010];
12
13 vector<int> vec[1010];
14
15 int Find(int x){
16     if(x==fa[x]) return x;
17     return fa[x]=Find(fa[x]);
18 }
19
20 int main()
21 {   int n,m,V;
22     scanf("%d%d%d",&n,&m,&V);
23     for(int i=1;i<=n;i++) scanf("%d",&c[i]);
24     for(int i=1;i<=n;i++) scanf("%d",&w[i]);
25     for(int i=1;i<=n;i++) fa[i]=i;
26     for(int i=0;i<m;i++){
27         int u,v;
28         scanf("%d%d",&u,&v);
29         u=Find(u),v=Find(v);
30         if(u!=v) fa[u]=v;
31     }
32     for(int i=1;i<=n;i++) vec[Find(i)].push_back(i);
33
34     for(int i=1;i<=n;i++){
35         if(fa[i]==i){
36             for(int j=V;j>=0;j--){
37                  int sumc=0,sumw=0;
38                  for(int k=0;k<vec[i].size();k++){
39                      sumc+=c[vec[i][k]];
40                      sumw+=w[vec[i][k]];
41                      if(j>=c[vec[i][k]]) dp[j]=max(dp[j],dp[j-c[vec[i][k]]]+w[vec[i][k]]);
42                     }
43                  if(j>=sumc) dp[j]=max(dp[j],dp[j-sumc]+sumw);
44             }
45         }
46     }
47     cout<<dp[V]<<endl;
48     return 0;
49 }

看不懂的代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<vector>
 5 using namespace std;
 6
 7 const int INF=0x3f3f3f3f;
 8 const int N=100010;
 9
10 int dp[2][1010];
11 int fa[1010],b[1010],w[1010];
12
13 vector<int> vec[1010];
14
15 int Find(int x){
16     if(x==fa[x]) return x;
17     return fa[x]=Find(fa[x]);
18 }
19
20 int main()
21 {   int n,m,x;
22     scanf("%d%d%d",&n,&m,&x);
23     for(int i=1;i<=n;i++) scanf("%d",&w[i]);
24     for(int i=1;i<=n;i++) scanf("%d",&b[i]);
25     for(int i=1;i<=n;i++) fa[i]=i;
26     for(int i=0;i<m;i++){
27         int u,v;
28         scanf("%d%d",&u,&v);
29         u=Find(u),v=Find(v);
30         if(u!=v) fa[u]=v;
31     }
32     for(int i=1;i<=n;i++)
33         vec[Find(i)].push_back(i);
34     int now=0;
35     for(int i=1;i<=n;i++){
36         now=!now;
37         int ww=0,bb=0;
38         for(int j=0;j<=x;j++) dp[now][j]=dp[!now][j];
39         for(int j=0;j<vec[i].size();j++){
40             int id=vec[i][j];
41             int www=w[id],bbb=b[id];
42             ww+=www,bb+=bbb;
43             for(int j=www;j<=x;j++)
44                dp[now][j]=max(dp[now][j],dp[!now][j-www]+bbb);
45         }
46         for(int j=ww;j<=x;j++)
47             dp[now][j]=max(dp[now][j],dp[!now][j-ww]+bb);
48     }
49     int ans=0;
50     for(int i=0;i<=x;i++) ans=max(ans,dp[now][i]);
51     cout<<ans<<endl;
52     return 0;
53 }

Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D

时间: 2024-08-02 06:58:30

Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D的相关文章

D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses 分组背包模板题

http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判断. 这样是不对的.因为这样使得同一组里面可能选择了两次. 3 0 2 1 2 3 1 1 3 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include &

http://codeforces.com/contest/741/problem/B B. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

题意: 给出上限体重W 然后还给出每个人的体重wi 和 魅力值 bi 互为伙伴的对(xi, yi) 可以凑成group 思路: 并查集找出所有的group 暴力背包 对于每一个group 要选出这一组内选一个人时的最优结果, 如果所有人的体重和小于等于W,还得考虑选所有人的情况 #include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include &l

Codeforces 741B Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么只能在连通块中选择一个,或者不选,为最大价值 [题解] 首先我们用并查集求出连通块,然后对连通块进行分组背包即可. [代码] #include <cstdio> #include <vector> #include <algorithm> #include <cstr

codeforces 742D Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses ——(01背包变形)

题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <vector> 5 using namespace std; 6 const int N = 1000 + 5;

并查集+背包 【CF741B】 Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

Descirption 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证总重量不超过W,剧场中的颜值最大能到多少? Input 第一行,三个整数n,m,w 第二行,n个整数w1,w2,...,wn 第三行,n个整数b1,b2,...,bn 接下来m行,每行表示一个关系,第i行有两个整数xi和yi. 每一组朋友关系都是不同的. Output ?一行,表示最大的魅力值总和.

CF741B Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses 并查集 01背包

title CF741B 简化题意: 有 \(n\) 个人 \((1<=n<=1000)\) ,每个人有一个重量 \(w_i(1\leqslant w_i\leqslant 1000)\) 和一个魅力值 \(b_i(1\leqslant b_i\leqslant 10^6)\) . \(n\) 个人之间有 \(m(1\leqslant m\leqslant min(\frac{n(n-1)}{2}, 10^5))\) 个关系.第 \(i\) 个关系由两个数字 \(x_i\) 和 \(y_i\)

#383 Div1 Problem B Arpa&#39;s weak amphitheater.... (分组背包 &amp;&amp; 并查集)

题意 : 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证总重量不超过W,剧场中的颜值最大能到多少? 分析 : 很显然的分组背包题目, 不过有所不同, 先来回顾一下普通的分组背包的描述 给出一个背包,背包有容量C,再给出N组物品,每组物品有ki种,每种物品有对应的体积Vi,价值Pi,每组物品至多选一种,且最多取一件.求用背包装物品,能获得的最大总价值是多少.可以发现和上

Arpa’s hard exam and Mehrdad’s naive cheat CodeForces - 742A (大水题)

适合初级程序员,只要注意不要遗漏情况就好. C语言代码如下: #include<stdio.h>#include<stdlib.h> int main(){    int n;    scanf("%d",&n);    if(n==0)        printf("1\n");    else if(n%4==1)        printf("8\n");    else if(n%4==2)       

cfodeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

题目链接:Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 第一次写\(dsu\ on\ tree\),来记录一下 \(dsu\ on\ tree\)主要维护子树信息,往往可以省掉一个数据结构的启发式合并.大体思路如下: 轻重链路径剖分之后,对每个点先递归处理他的所有轻儿子,每次处理完轻儿子之后把这棵子树的信息清空.最后再来处理重孩子,重儿子的信息就可以不用清空了.由于我们是用一个全局数组来记录信息的,重儿子子树的信息就仍然保留