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

【题目链接】 http://codeforces.com/problemset/problem/741/B

【题目大意】

  给出一张图,所有连通块构成分组,每个点有价值和代价,
  要么选择整个连通块,要么只能在连通块中选择一个,或者不选,为最大价值

【题解】

  首先我们用并查集求出连通块,然后对连通块进行分组背包即可。

【代码】

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#define rep(i,n) for(int i=1;i<=n;i++)
using namespace std;
const int N=1010;
int dp[N],f[N],n,m,x,y,size,w[N],b[N];
vector<int> v[N];
int sf(int x){return f[x]==x?x:f[x]=sf(f[x]);}
int main(){
    while(~scanf("%d%d%d",&n,&m,&size)){
        rep(i,n)f[i]=i,v[i].clear();
        rep(i,n)scanf("%d\n",&w[i]);
        rep(i,n)scanf("%d\n",&b[i]);
        rep(i,m){scanf("%d%d",&x,&y);f[sf(x)]=sf(y);}
        rep(i,n)v[sf(i)].push_back(i);
        memset(dp,0,sizeof(dp));
        rep(i,n)if(sf(i)==i){
            for(int j=size;j>=0;j--){
                int W=0,B=0;
                for(int k=0;k<v[i].size();k++){
                    W+=w[v[i][k]]; B+=b[v[i][k]];
                    if(j>=w[v[i][k]])dp[j]=max(dp[j],dp[j-w[v[i][k]]]+b[v[i][k]]);
                }if(j>=W)dp[j]=max(dp[j],dp[j-W]+B);
            }
        }printf("%d\n",dp[size]);
    }return 0;
}   

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

时间: 2024-12-18 00:04:46

Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses的相关文章

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;

Arpa&#39;s weak amphitheater and Mehrdad&#39;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 grou

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

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 &

并查集+背包 【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,每组物品至多选一种,且最多取一件.求用背包装物品,能获得的最大总价值是多少.可以发现和上

CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <c

CodeForces 742A Arpa’s hard exam and Mehrdad’s naive cheat

题意:求1378 n次幂的最后一位. 析:两种方法,第一种,就是快速幂,第二种找循环节,也很好找,求一下前几个数就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #