codevs 3372 选学霸(hash+并查集+多重背包)
先通过并查集处理出来有多少种不同的集合,每一个集合有多少人。一定要不要忘记了与别的没有联系的独立点。
并查集的时候能够通过hash处理出来每一个数目同样的集合的个数。
这样以人数为权值。个数为限制进行多重背包,结果就是答案。
题目链接:http://codevs.cn/problem/3372/
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
#define LL long long
//#define LL long long
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007 const int maxn = 30010; using namespace std; int vis1[maxn];
int vis2[maxn];
int vis[maxn];
int num[maxn]; int dp[2*maxn];
int fa[maxn]; struct node
{
int snum;
int sum;
} p[maxn]; int n;
void init()
{
for(int i = 0; i <= n; i++) fa[i] = i;
memset(vis1, 0, sizeof(vis1));
memset(vis2, 0, sizeof(vis2));
memset(dp, 0, sizeof(dp));
memset(vis, 0, sizeof(vis));
} int Find(int x)
{
if(x != fa[x]) fa[x] = Find(fa[x]);
return fa[x];
} void add(int x, int y)
{
int x1, y1;
x1 = Find(x);
y1 = Find(y);
if(x1 != y1) fa[x1] = y1;
} int main()
{
int m, k;
while(~scanf("%d %d %d",&n, &m, &k))
{
init();
int x, y;
for(int i = 0; i < k; i++)
{
scanf("%d %d",&x, &y);
vis[x] = 1;
vis[y] = 1;
add(x, y);
}
int ans = 0;
int xsum = 0;
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
xsum ++;
continue;
}
int s = Find(i);
vis1[s] ++;
}
for(int i = 1; i <= n; i++)
{
if(!vis1[i]) continue;
num[ans++] = vis1[i];
}
sort(num, num+ans);
for(int i = 0; i < ans; i++) vis2[num[i]]++;
int cnt = 0;
for(int i = 1; i <= n; i++)
{
if(!vis2[i]) continue;
p[cnt].snum = i;
p[cnt++].sum = vis2[i];
}
int v = 2*(m+1);
dp[0] = 1;
for(int i = 0; i < cnt; i++)
{
for(int j = v; j >= 0; j--)
{
if(!dp[j]) continue;
for(int kk = 1; kk <= p[i].sum; kk++)
{
if(kk*p[i].snum+j > v) break;
if(dp[kk*p[i].snum+j]) break;
dp[kk*p[i].snum+j] = 1;
}
}
}
int lx, rx;
for(int i = m; i >= 0; i--)
{
if(dp[i])
{
lx = i;
break;
}
}
for(int i = m; i <= 2*(m+1); i++)
{
if(dp[i])
{
rx = i;
break;
}
}
lx = max(lx, 0);
rx = min(rx, 2*(m+1));
int sx = abs(m-lx);
int sy = abs(rx-m);
if(sx <= xsum)
{
sx = 0;
lx = m;
}
else
{
sx -= xsum;
lx += xsum;
}
if(sy < sx)
{
cout<<rx<<endl;
continue;
}
cout<<lx<<endl;
}
return 0;
}
/*
10 4 9
8 2
1 5
5 10
9 7
10 3
3 4
4 6
8 9
6 8
0 5 3 3
1 2
2 3
3 4
4 */
codevs 3372 选学霸(hash+并查集+多重背包)的更多相关文章
- codevs 3372 选学霸
3372 选学霸 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- codevs——3372 选学霸(背包)
题目等级 : 大师 Master 时间限制: 1 s 空间限制: 128000 KB 题解 题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实 ...
- Luogu P2170选学霸【并查集+背包】By cellur925
题目传送门 开始看到本题完全认为就是个彻头彻尾的并查集,只要把实力相当的人都并到一个集合中,最后再找一共有多少联通块即可. 后来发现这是大错特错的qwq.因为选了一个集合中的某人,那这个集合中所有人就 ...
- 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 ...
- Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)
<题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...
- CSU 1326:The contest(并查集+分组背包)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 题意:…… 思路:并查集建图处理出边,然后分组背包. 之前不会分组背包,比赛的时候也推不出来 ...
- 1326: The contest(并查集+分组背包)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 殷犇有很多队员.他们都认为自己是最强的,于是,一场比赛开始了~ 于是安叔主办了一场比赛,比赛 ...
- Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)
题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...
随机推荐
- Fibbonacci Number(杭电2070)
/*Fibbonacci Number Problem Description Your objective for this question is to develop a program whi ...
- gdb help all 帮助信息
Command class: aliases ni -- Step one instruction rc -- Continue program being debugged but run it i ...
- MYSQL Training: MySQL I
让以admin身份登录.源代码: 非常easy的注入 在username输入 admin' OR '1'='1 OK.
- ES JVM使用如果超过75%就会GC较多,导致ES索引性能下降
转自:https://www.elastic.co/guide/en/cloud/current/ec-metrics-memory-pressure.html Scenario: How Does ...
- Spring mvc 实现jsonp和json数据类型
在使用springmvc开发rest接口的时候很方便,可以直接使用@ResponseBody注解,直接加在springmvc的控制器类的方法上,springmvc会直接为我们将返回的对 ...
- element-ui自定义table表头,修改列标题样式
elementUI table表格一般的样式是这样的: 但是要改变表头是比较麻烦的一个事情,但是往往有些项目是需要的比如改成如下样式: 一般直接改起来挺麻烦,好在官网提供了一个方法:render-he ...
- js点击时关闭该范围下拉菜单之外的菜单
$(function(){ $(document).bind("click",function(e){ //id为menu的是菜单 if($(e.target).closest(& ...
- Unity的SendMessage方法
用法(该对象所有脚本都能收到): gameObject.SendMessage("要执行的方法名"); 通知的另一种实现: gameObject.GetComponent<脚 ...
- ZBrush中的实时遮罩
在ZBrush®中有许多遮罩类型,包括柔滑遮罩.反转遮罩,实时遮罩等.其中,实时遮罩又包含很多种类,它不同于一般的遮罩是不显示的,实时遮罩是根据实时信息产生新的遮罩. 在“Brush”菜单下“Auto ...
- 路飞学城Python-Day49
55-善于使用父亲的padding,而不是margin 56-文本属性和字体属性 div{ width: 300px; height: 100px; border: 1px solid red; /* ...