CF2.D 并查集+背包
1 second
256 megabytes
standard input
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.
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.
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.
3 1 5
3 2 5
2 4 2
1 2
6
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
7
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.
题意:
有n个女孩,每个女孩有重量和美丽度,有m对朋友关系,朋友关系可传递。邀请他们参加聚会,每一个朋友圈的人要么全部参加,要么不参加,要么只有一个人参加。问在不超过总重量w的条件下最大美丽度是多少。
代码:
//显然背包,分两种物品,单个的人,整个集合的人。
#include<bits\stdc++.h>
using namespace std;
long long dp[];
int hm[],hd[],vis[],fat[];
int n,m,w;
vector<int>L[];
int find(int x)
{
if(fat[x]!=x)
fat[x]=find(fat[x]);
return fat[x];
}
void connect(int x,int y)
{
int xx=find(x),yy=find(y);
if(xx!=yy)
fat[yy]=xx;
}
void init()
{
for(int i=;i<=n;i++)
fat[i]=i;
}
int main()
{
scanf("%d%d%d",&n,&m,&w);
for(int i=;i<=n;i++)
scanf("%d",&hm[i]);
for(int i=;i<=n;i++)
scanf("%d",&hd[i]);
int x,y;
init();
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
connect(x,y);
}
memset(vis,,sizeof(vis));
int cnt=;
for(int i=;i<=n;i++)
{
int tem=find(i);
if(!vis[tem])
{
L[++cnt].push_back(tem);
if(tem!=i)
L[cnt].push_back(i);
vis[tem]=cnt;
}
else if(vis[tem]&&tem!=i)
{
L[vis[tem]].push_back(i);
}
}
memset(dp,,sizeof(dp));
for(int i=;i<=cnt;i++)
{
int len=L[i].size();
long long sum1=,sum2=;
for(int j=;j<len;j++)
{
sum1+=hm[L[i][j]];
sum2+=hd[L[i][j]];
}
for(int k=w;k>=;k--)
{
if(k>=sum1) //整个集合的
dp[k]=max(dp[k],dp[k-sum1]+sum2);
for(int j=;j<len;j++) //单个的
{
int tem=L[i][j];
if(k>=hm[tem])
dp[k]=max(dp[k],dp[k-hm[tem]]+hd[tem]);
}
}
}
cout<<dp[w]<<endl;
return ;
}
CF2.D 并查集+背包的更多相关文章
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- POJ - 1417 并查集+背包
思路:很简单的种类并查集,利用并查集可以将所有的人分成几个集合,每个集合又分为好人和坏人集合,直接进行背包dp判断有多少种方法可以在取了所有集合并且人数正好凑足p1个好人的方案.dp(i, j)表示前 ...
- poj1417(带权并查集+背包DP+路径回溯)
题目链接:http://poj.org/problem;jsessionid=8C1721AF1C7E94E125535692CDB6216C?id=1417 题意:有p1个天使,p2个恶魔,天使只说 ...
- 并查集+背包 【CF741B】 Arpa's weak amphitheater and Mehrdad's valuable Hoses
Descirption 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证 ...
- poj1417 True Liars[并查集+背包]
有一点小转化的题,在设计dp状态时还是有点费脑筋的. 地址. 依题意,首先可以知道肯定要扩展域的并查集(明摆着的嘛).一个"好人"域,一个"坏人"域,每句话分两 ...
- Luogu P2170选学霸【并查集+背包】By cellur925
题目传送门 开始看到本题完全认为就是个彻头彻尾的并查集,只要把实力相当的人都并到一个集合中,最后再找一共有多少联通块即可. 后来发现这是大错特错的qwq.因为选了一个集合中的某人,那这个集合中所有人就 ...
- 西安邀请赛-D(带权并查集+背包)
题目链接:https://nanti.jisuanke.com/t/39271 题意:给定n个物品,m组限制,每个物品有个伤害值,现在让两个人取完所有物品,要使得两个人取得物品伤害值之和最接近,输出伤 ...
- poj 1417 True Liars(并查集+背包dp)
题目链接:http://poj.org/problem?id=1417 题意:就是给出n个问题有p1个好人,p2个坏人,问x,y是否是同类人,坏人只会说谎话,好人只会说实话. 最后问能否得出全部的好人 ...
- 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 而且如果邀请 ...
随机推荐
- 第3章 拍摄UFO——单一职责原则
就一个类而言,应该仅有一个引起它变化的原因
- windows+ant+git+tomcat中ant直接获取git项目部署注意点
最近项目搬迁到公司的"GitHub"上面原来的SVN的ant发布脚本要改下,于是百度ant获取git的方法太少了,windows平台上更是没有所以搞了两天,今天终于有点成果分享给大 ...
- Object-c 内存管理
内存管理 主要内容 1.内存管理的概念 2.引用计数 3.如何持有对象所有权 4.自动释放池 5.@property的使用 什么是内存管理 内存管理是关于如何管理对象生 ...
- 3ds max 渲染清晰面片的边缘
3ds max的菜单栏 -> 渲染 -> 材质编辑器->精简材质编辑器,将面状打勾,如下图,就能渲染出面片清晰的图形.
- (转)Redis使用场景及使用经验
Redis is an open source (BSD licensed), in-memory data structure store! 刚刚结束一个游戏类的活动项目,由于预估的参与人数较多,产 ...
- Ajax请求跨域问题 -- 转载
几乎每种浏览器都存在默认的安全机制,都有同源策略,因为浏览器恶意的把每个外部请求的都当做是黑客攻击,相当于是对自身的保护,所以浏览器在运行脚本时会判断脚本与请求的页面是否是同一来源,这个同一来源,包括 ...
- 【Java EE 学习 67 上】【OA项目练习】【JBPM工作流的使用】
OA项目中有极大可能性使用到JBPM框架解决流程控制问题,比如请假流程.报销流程等等. JBPM:JBoss Business Process Management,翻译过来就是业务流程管理.实际上就 ...
- HP-SOCKET TCP/UDP通信框架库解析
项目概述: HP-SOCKET是一套通用TCP/UDP通信框架,包括服务器.客户端.Agent组件:其目标是提供高性能.通用性.简易性.可扩展.可定制: 鉴于此,其仅实现基本的通用框架通信.数据收发功 ...
- C++ 系列:编译 boost
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- 修改MS SQL忽略大小写 分类: SQL Server 数据库 2015-06-19 09:18 34人阅读 评论(0) 收藏
第一步:数据库->属性->选项->限制访问:SINGLE_USER 第二步:ALTER DATABASE [数据库名称] collate Chinese_PRC_CI_AI 第三步: ...