Codeforces Round #383 (Div. 2) D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 并查集+双重01背包

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 nm and w (1  ≤  n  ≤  10001 ≤ 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.




Source

Codeforces Round #383 (Div. 2)


My Soluton

题意:一堆人,这些人构成一些集合,2个元素至少有一条路径则为同一个集合,对于这些集合每个交合要么全取要不去不超过一个人,且每个人有一个wi和ai,要求在总wi小于等于w的情况下,是总ai最大


并查集+双重01背包

先用并查集处理出ptr-1个集合,然后对于这ptr-1个集合要么全取要么去不大于1个,跑一边01背包。

dp[i][j]表示到 第i个背包是,总消耗容量j是最多装 dpij的东西。

复杂度 O(N^2)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int, int> ii;
const int maxn = 1e3 + 8;

int father[maxn], _rank[maxn];

inline void DisjointSet(int n)
{
    for(int i = 0; i <= n; i++){
        father[i] = i;
    }
}

inline int _find(int v)
{
    return father[v] = father[v] == v ? v : _find(father[v]);
}

inline int _merge(int x, int y)
{
    int a = _find(x), b = _find(y);
    if(_rank[a] < _rank[b]){
        father[a] = b;
    }
    else{
        father[b] = a;
        if(_rank[a] == _rank[b]){
            _rank[a]++;
        }
    }
}

int w[maxn], a[maxn], sz[maxn], sum[maxn];
map<int, vector<ii> > mp;
map<int, int> Ind;
int dp[maxn][maxn];

int main()
{
    #ifdef LOCAL
    freopen("d.txt", "r", stdin);
    //freopen("d.out", "w", stdout);
    int T = 2;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    int n, m, sumw, findi;
    cin >> n >> m >> sumw;
    DisjointSet(n);
    for(int i = 1; i <= n; i++){
        cin >> w[i];
    }
    for(int i = 1; i <= n; i++){
        cin  >> a[i];
    }

    int u, v;
    while(m--){
        cin >> u >> v;
        if(_find(u) != _find(v)) _merge(u, v);
    }

    for(int i = 1; i <= n; i++){
        findi = _find(i);
        sz[findi] += w[i];
        sum[findi] += a[i];
        mp[findi].push_back(ii(w[i], a[i]));
    }

    //new a[maxn] and w[maxn]
    int ptr = 1;
    for(int i = 1; i <= n; i++){
        if(sz[i] > 0){
            w[ptr] = sz[i];
            a[ptr] = sum[i];
            Ind[ptr] = i;
            //cout << sz[i] << " " << sum[i] << endl;
            ptr++;
        }
    }

    int i, j;
    for(i = 1; i < ptr; i++){
        for(j = 0; j <= sumw; j++){
            dp[i][j] = dp[i-1][j];
            if(j != 0){
                dp[i][j] = max(dp[i][j], dp[i][j-1]);
            }
            if(j - w[i] >= 0){
                dp[i][j] = max(dp[i][j], dp[i-1][j-w[i]] + a[i]);
            }
            //else dp[j][0] = max(dp[i][j][0], max(dp[i-1][j][0], dp[i-1][j][1]));
            for(auto k = mp[Ind[i]].begin(); k != mp[Ind[i]].end(); k++){
                if(j - (k->first) >= 0) dp[i][j] = max(dp[i][j], dp[i-1][j-(k->first)] + (k->second));
            }
        }
    }

    cout << dp[ptr-1][sumw] << endl;


    #ifdef LOCAL
    memset(_rank, 0, sizeof _rank);
    memset(sz, 0, sizeof sz);
    memset(sum, 0, sizeof sum);
    memset(dp, 0, sizeof dp);
    mp.clear();
    Ind.clear();
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}



  Thank you!

                                                                                                                                               ------from ProLights

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值