并查集+背包 【CF741B】 Arpa's weak amphitheater and Mehrdad'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
一行,表示最大的魅力值总和。
不错的一个题。
考察:并查集+分组背包。
我们先并查集将在一组的人处理出来.
顺便处理一下每个组的编号与组中每个人是谁.
\(belong[x]\)则代表\(x\)组的编号。
我们用\(ins[i][j]\)代表第\(i\)组的第\(j\)个元素是谁。
然后每一个小组可以全部去,可以去一个,可以不去。
考虑问题转化,我们将这一个小组看成一个物品,直接把其价值及体积加入到当前组即可。
处理完之后,跑分组背包即可。
需要注意的一点是:如果某一组只有一个元素,我们不能再将其看为一个物品重新加入(克隆?)
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#define R register
using namespace std;
const int gz=1008;
inline void in(int &x)
{
int f=1;x=0;char s=getchar();
while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}
while(isdigit(s)){x=x*10+s-'0';s=getchar();}
x*=f;
}
int n,m,w,wi[gz],f[gz],b[gz],dp[gz];
int find(R int x){return f[x]==x?x:f[x]=find(f[x]);}
int belong[gz],cnt;
int ins[gz][gz],W[gz],B[gz];
int main()
{
in(n),in(m),in(w);
for(R int i=1;i<=n;i++)in(wi[i]);
for(R int i=1;i<=n;i++)in(b[i]);
for(R int i=1;i<=n;i++)f[i]=i;
for(R int i=1,x,y;i<=m;i++)
{
in(x),in(y);
R int fa=find(x);
R int fb=find(y);
if(fa==fb)continue;
f[fa]=fb;
}
for(R int i=1;i<=n;i++)
{
R int x=find(i);
if(!belong[x])belong[x]=++cnt;
ins[belong[x]][++ins[belong[x]][0]]=i;
W[belong[x]]+=wi[i];
B[belong[x]]+=b[i];
}
for(R int i=1;i<=cnt;i++)
{
if(ins[i][0]!=1)
{
ins[i][++ins[i][0]]=n+1;
wi[n+1]=W[i];
b[n+1]=B[i];
}
for(R int j=w;j>=0;j--)
for(R int k=1;k<=ins[i][0];k++)
if(j>=wi[ins[i][k]])
dp[j]=max(dp[j],dp[j-wi[ins[i][k]]]+b[ins[i][k]]);
}
printf("%d",dp[w]);
}
并查集+背包 【CF741B】 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(01背包)
题目链接:http://codeforces.com/contest/742/problem/D D. Arpa's weak amphitheater and Mehrdad's valuable ...
- 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 ...
- 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 ...
- Arpa's weak amphitheater and Mehrdad's valuable Hoses
Arpa's weak amphitheater and Mehrdad's valuable Hoses time limit per test 1 second memory limit per ...
- Codeforces Round #383 (Div. 2)D. Arpa's weak amphitheater and Mehrdad's valuable Hoses(dp背包+并查集)
题目链接 :http://codeforces.com/contest/742/problem/D 题意:给你n个女人的信息重量w和美丽度b,再给你m个关系,要求邀请的女人总重量不超过w 而且如果邀请 ...
- 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 个人是一个 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
<题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
- Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)
题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...
- codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)
题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...
随机推荐
- [洛谷P1879][USACO06NOV]玉米田Corn Fields
题目大意:有一个$n\times m$的矩阵,$(1 \leq m \leq 12; 1 \leq n \leq 12)$,想在其中的一些格子中种草,一些格子不能种草,且两块草地不相邻.问有多少种种植 ...
- poj 3422 洛谷P2045 K取方格数(方格取数加强版)
Description: 给出一个n*n的矩阵,每一格有一个非负整数Aij,(Aij <= 1000)现在从(1,1)出发,可以往右或者往下走,最后到达(n,n),每达到一格,把该格子的数取出来 ...
- Website Collection
前一百个卡特兰数 Candy?的博弈论总结 杜教筛资料 线性基资料 (ex)BSGS资料 斐波那契数列前300项 斯特林数 STL标准库-容器-unordered_set C++ unordered_ ...
- spring中Constructor、@Autowired、@PostConstruct的顺序【转】
其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...
- 第九届蓝桥杯C/C++B组题解附代码
1.标题:第几天 2000年的1月1日,是那一年的第1天.那么,2000年的5月4日,是那一年的第几天? 125天 打开日历就ok 2. 标题:明码 汉字的字形存在于字库中,即便在今天,16点阵的字库 ...
- C# windows application Hello World
创建一个Windows application项目,然后可以调用里面的工具来生成代码. using System; using System.Collections.Generic; using Sy ...
- Cannot load project: com.intellij.ide.plugins.PluginManager$StartupAbortedException
今天电脑突然蓝屏,idea异常关闭,开机重启后,打开idea,点击项目出现 Cannot load project: com.intellij.ide.plugins.PluginManager$St ...
- charles 踩坑记录
charles破解教程:http://www.jianshu.com/p/12e75eb8f53d 1.需注意软件和破解脚本的版本是否正确(例如3.x.x版本的破解脚本不能用于4.x.x版本的char ...
- oracle导入和导出和授权
导入数据库: imp demo@orcl file=d:/bak_1023.dmp full=y ignore=y 导出数据库: @orcl file=d:/bak_1023.dmpexp yhtj/ ...
- 最短路径算法 SP
dijkstra求最短路径长度 dijkstra求最短路并记录路径 #include<stdio.h> #include<string.h> #include<stack ...