CSU1326+背包+并查集
先预处理出有多少个任务即可
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxn = ;
const int maxm = ;
struct Edge{
int u,v,next;
}edge[ maxm ];
int cnt ,head[ maxn ];
void init(){
cnt = ;
memset( head,-,sizeof( head ) );
}
void addedge( int a,int b ){
edge[ cnt ].u = a;
edge[ cnt ].v = b;
edge[ cnt ].next = head[ a ];
head[ a ] = cnt ++;
}
int fa[ maxn ],root[ maxn ],dp[ maxn ];
int val[ maxn ],cost[ maxn ];
int find( int x ){
if( x==fa[x] ) return x;
else return fa[x] = find( fa[x] );
}
int main(){
int n,Max,k;
while( ~scanf("%d%d%d",&n,&Max,&k) ){
for( int i=;i<=n;i++ ){
scanf("%d%d",&val[i],&cost[i]);
fa[ i ] = i;
}
init();
memset( dp,,sizeof( dp ) );
memset( root,,sizeof( root ) );
while( k-- ){
int a,b;
scanf("%d%d",&a,&b);
int x = find(a);
int y = find(b);
if( fa[x]!=y ){
fa[x] = y;
}
}
int r = ;
for( int i=;i<=n;i++ ){
int x = find( i );
if( root[x]== ){
root[x] = r++;
}
addedge( root[x],i );
}
for( int i=;i<r;i++ ){
for( int j=Max;j>=;j-- ){
for( int k=head[i];k!=-;k=edge[k].next ){
int nxt = edge[k].v;
if( j>=cost[nxt] ){
dp[j] = max( dp[j],dp[j-cost[nxt]]+val[nxt] );
}
}
}
}
printf("%d\n",dp[Max]);
}
return ;
}
CSU1326+背包+并查集的更多相关文章
- 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 个人是一个 ...
- vijos 1250 最勇敢的机器人 分组背包+并查集
P1250最勇敢的机器人 背景 Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ 描述 机器人们都想知道谁是最勇敢的,于是它们比赛搬运一些物品. 它们到了一个仓库,里面有n ...
- 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 而且如果邀请 ...
- #383 Div1 Problem B Arpa's weak amphitheater.... (分组背包 && 并查集)
题意 : 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证总重量不超过W ...
- 【春训团队赛第四场】补题 | MST上倍增 | LCA | DAG上最长路 | 思维 | 素数筛 | 找规律 | 计几 | 背包 | 并查集
春训团队赛第四场 ID A B C D E F G H I J K L M AC O O O O O O O O O 补题 ? ? O O 传送门 题目链接(CF Gym102021) 题解链接(pd ...
- F - True Liars - poj1417(背包+并查集)
题意:有这么一群人,一群好人,和一群坏人,好人永远会说实话,坏人永远说假话,现在给你一组对话和好人与坏人的数目P1, P2. 数据里面的no是A说B是坏人, yes代表A说B是好人,就是这样,问题能不 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包
A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...
- poj1417 带权并查集+0/1背包
题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是 ...
随机推荐
- Swift中的循环语句
循环语句能够使程序代码重复执行.Swift编程语言支持4种循环构造类型:while.do while.for和for in.for和while循环是在执行循环体之前测试循环条件,而do while是在 ...
- 在swift中使用MJRefresh
cocoapod导入的,并且桥接已经完成,但是就是不提示方法,醉了,
- 大家注意:升级 win8.1 火狐浏览器 谷歌浏览器 搜狗五笔输入法 都不能用啦
大家注意:升级 win8.1 火狐浏览器 谷歌浏览器 搜狗五笔输入法 都不能用啦 我的电脑64位 win8 thinkpad e531,8G内存 刚在线升级完8.1,发现这些问题,大家注意,有知道问题 ...
- encodeURIComponent=>Uri.EscapeDataString
javascript: encodeURIComponent=> C#: Uri.EscapeDataString http://stackoverflow.com/questions/5754 ...
- ALI OSS RequestTimeTooSkewed
php版阿里oss sdk,请求时抛RequestTimeTooSkewed错误,说时间差距太大,搜了一下发现是服务器的时间设置问题. 我们在安装完Centos Linux操作系统之后,点击系统的时间 ...
- TextBox 绑定到DataTable某一列属性上
将TextBox绑定到DataTable某一列属性上 DataTable dt = GetDataTable() textBox1.DataBindings.Add("Text", ...
- nginx 重启命令
#重启nginx sudo /etc/init.d/nginx restart sudo /etc/init.d/nginx Usage: /etc/init.d/nginx {start|stop| ...
- Linux下mail/mailx命令发送邮件
最近看到项目中经常会用mail/mailx命令发送由java程序生成的report,比较新鲜.下面就简单介绍下mail/mailx命令用法.本文以mail命令举例(mail/mailx)效果都是一样的 ...
- 深入浅出话XAML-学习笔记
第一章 XAML是什么? 1.1XAML之前 *设计师的设计更不上程序逻辑的变化 *程序员未能完全实现设计师提供的效果图 *效果图与程序功能不能完全匹配 *从效果图到软件UI的转化耗费很多时间 1.2 ...
- response 后刷新页面,点击按钮后,禁用该按钮
一,正常的点击按钮后,将其灰显,全部执行完毕再正常显示. this.btnSave.Attributes.Add("onclick", "if (typeof(Page_ ...