codeforce 742 D. Arpa's weak amphitheater and Mehrdad's valuable Hoses (分组背包+并查集))
2016-12-17 13:08
806 查看
有关系的并查集分在一组~01背包~每组选一个或全选~更新最优解即可~~
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.
![](http://codeforces.com/predownloaded/55/47/55473ede845077f493c659c64bcefa1e8986771d.png)
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,
![](http://codeforces.com/predownloaded/f6/00/f600ecbaf532872e2bd9bf6c7ec0c54bda759f0d.png)
,
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
Output
Input
Output
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<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 1e3 + 100;
struct node
{
int v,w;
}a
;
vector<node>vec
;
int fa
,dp
;
int find(int x)
{
int r=x;
while(fa[r]!=r) r=fa[r];
int i=x,j;
while(i!=r) {
j=fa[i];
fa[i]=r;
i=j;
}
return r;
}
int main()
{
int n,v,w,i,j,m,t1,t2,k,V,W;
cin>>n>>m>>w;
for(i=1;i<=n;i++) cin>>a[i].v; //体积
for(i=1;i<=n;i++) cin>>a[i].w; //价值
for(i=1;i<=n;i++) fa[i]=i;
while(m--) {
cin>>t1>>t2;
if(t1>t2) swap(t1,t2);
int fx=find(t1),fy=find(t2);
if(fx!=fy) fa[fy]=fx;
}
for(i=1;i<=n;i++) {
vec[find(i)].push_back(a[i]);
}
node tmp,t;
for(i=1;i<=n;i++) {
if(find(i)==i) {
tmp.v=tmp.w=0;
for(j=0;j<vec[i].size();j++) {
t=vec[i][j];
tmp.v+=t.v,tmp.w+=t.w;
}
vec[find(i)].push_back(tmp);
}
}
for(i=1;i<=n;i++) {
if(find(i)==i)
for(j=w;j>=0;j--) {
for(int z=0;z<vec[find(i)].size();z++) {
tmp=vec[i][z];
if(tmp.v<=j) dp[j]=max(dp[j],dp[j-tmp.v]+tmp.w);
}
}
}
cout<<dp[w]<<endl;
return 0;
}
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.
![](http://codeforces.com/predownloaded/55/47/55473ede845077f493c659c64bcefa1e8986771d.png)
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,
![](http://codeforces.com/predownloaded/f6/00/f600ecbaf532872e2bd9bf6c7ec0c54bda759f0d.png)
,
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 66 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<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 1e3 + 100;
struct node
{
int v,w;
}a
;
vector<node>vec
;
int fa
,dp
;
int find(int x)
{
int r=x;
while(fa[r]!=r) r=fa[r];
int i=x,j;
while(i!=r) {
j=fa[i];
fa[i]=r;
i=j;
}
return r;
}
int main()
{
int n,v,w,i,j,m,t1,t2,k,V,W;
cin>>n>>m>>w;
for(i=1;i<=n;i++) cin>>a[i].v; //体积
for(i=1;i<=n;i++) cin>>a[i].w; //价值
for(i=1;i<=n;i++) fa[i]=i;
while(m--) {
cin>>t1>>t2;
if(t1>t2) swap(t1,t2);
int fx=find(t1),fy=find(t2);
if(fx!=fy) fa[fy]=fx;
}
for(i=1;i<=n;i++) {
vec[find(i)].push_back(a[i]);
}
node tmp,t;
for(i=1;i<=n;i++) {
if(find(i)==i) {
tmp.v=tmp.w=0;
for(j=0;j<vec[i].size();j++) {
t=vec[i][j];
tmp.v+=t.v,tmp.w+=t.w;
}
vec[find(i)].push_back(tmp);
}
}
for(i=1;i<=n;i++) {
if(find(i)==i)
for(j=w;j>=0;j--) {
for(int z=0;z<vec[find(i)].size();z++) {
tmp=vec[i][z];
if(tmp.v<=j) dp[j]=max(dp[j],dp[j-tmp.v]+tmp.w);
}
}
}
cout<<dp[w]<<endl;
return 0;
}
相关文章推荐
- Arpa's weak amphitheater and Mehrdad's valuable Hoses CodeForces - 742D (分组背包)
- codeforce 741 B. Arpa's weak amphitheater and Mehrdad's valuable Hoses(背包 dp)
- codeforces 742d D - Arpa's weak amphitheater and Mehrdad's valuable Hoses
- E - Arpa's weak amphitheater and Mehrdad's valuable Hoses (分组背包)
- Codeforces Round #383 (Div. 1) Problem B.Arpa's weak amphitheater and Mehrdad's valuable Hoses 解题报告
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 并查集+双重01背包
- Codeforces 741B-Arpa's weak amphitheater and Mehrdad's valuable Hoses
- Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(DP)
- Codeforces Round #383 (Div. 2)-D. Arpa's weak amphitheater and Mehrdad's valuable Hoses
- E - Arpa's weak amphitheater and Mehrdad's valuable Hoses
- codeforce 742 E. Arpa’s overnight party and Mehrdad’s silent entering (分食物||二分图染色+dfs)
- Arpa's weak amphitheater and Mehrdad's valuable Hoses
- Round 3 E-Arpa's weak amphitheater and Mehrdad's valuable Hoses( 并查集 分组背包)
- Codeforces Round #383 (Div. 2)D.Arpa's weak amphitheater and Mehrdad's valuable Hoses【并查集+分组背包】
- 【42.86%】【codeforces 742D】Arpa's weak amphitheater and Mehrdad's valuable Hoses
- 742 C. Arpa's loud Owf and Mehrdad's evil plan codeforces
- 【Codeforces 741 B. Arpa's weak amphitheater and Mehrdad's 】+ 并查集 + 01背包
- 742 A - Arpa’s hard exam and Mehrdad’s naive cheat codeforces
- 742 B - Arpa’s obvious problem and Mehrdad’s terrible solution codeforces
- Codeforces Round #383 (Div. 2)-C. Arpa's loud Owf and Mehrdad's evil plan