先通过并查集处理出来有多少种不同的集合,每一个集合有多少人。一定要不要忘记了与别的没有联系的独立点。

并查集的时候能够通过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+并查集+多重背包)的更多相关文章

  1. codevs 3372 选学霸

    3372 选学霸  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果 ...

  2. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  3. codevs——3372 选学霸(背包)

    题目等级 : 大师 Master  时间限制: 1 s  空间限制: 128000 KB 题解       题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实 ...

  4. Luogu P2170选学霸【并查集+背包】By cellur925

    题目传送门 开始看到本题完全认为就是个彻头彻尾的并查集,只要把实力相当的人都并到一个集合中,最后再找一共有多少联通块即可. 后来发现这是大错特错的qwq.因为选了一个集合中的某人,那这个集合中所有人就 ...

  5. 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 ...

  6. Codeforces 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses (并查集+分组背包)

    <题目链接> 题目大意: 就是有n个人,每个人都有一个体积和一个价值.这些人之间有有些人之间是朋友,所有具有朋友关系的人构成一组.现在要在这些组中至多选一个人或者这一组的人都选,在总容量为 ...

  7. CSU 1326:The contest(并查集+分组背包)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 题意:…… 思路:并查集建图处理出边,然后分组背包. 之前不会分组背包,比赛的时候也推不出来 ...

  8. 1326: The contest(并查集+分组背包)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 殷犇有很多队员.他们都认为自己是最强的,于是,一场比赛开始了~ 于是安叔主办了一场比赛,比赛 ...

  9. Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)

    题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...

随机推荐

  1. 怎样在Java中运行Hive命令或HiveQL

    这里所说的在Java中运行Hive命令或HiveQL并非指Hive Client通过JDBC的方式连接HiveServer(or HiveServer2)运行查询,而是简单的在部署了HiveServe ...

  2. Android通过泛型简化findViewById类型转换

    曾经老用findViewById,每次使用还得add cast一下今天看到一个视频(依据视频中使用的IDE判断,应该是几年前的视频了..),使用了一个方法,能够不用每次使用findViewById都去 ...

  3. UVA - 12230 Crossing Rivers 概率期望

    You live in a village but work in another village. You decided to follow the straight path between y ...

  4. Linux命令学习-curl

    作用 curl是利用URL语法的一款强大的网络工具,你可以使用它完成上传下载文件等操作. curl http://www.cnblogs.com 上诉的命令即可将页面内容打印到屏幕上. 常用参数 -o ...

  5. JAVA-mysql读写分离插件介绍

    kingshard是一个由Go开发高性能MySQL Proxy项目,kingshard在满足基本的读写分离的功能上,致力于简化MySQL分库分表操作:能够让DBA通过kingshard轻松平滑地实现M ...

  6. gcc编译c中有与lua交互的代码

    编译C程序中有与Lua有关的程序(编译环境是Linux系统,lua解释器是luajit)gcc -o test30 test30.cpp -I/usr/local/include/luajit-2.0 ...

  7. shell-6.环境变量配置文件

    1. 2. 3. 4. 5. 6.

  8. Unity 移动键Q的三种用法 For Mac,Windows类同

    拖动整个场景:三指 (任何模式下)ALT+三指:旋转当前镜头 (任何模式下)双指前后滑动:缩放镜头 ps1:Q键移动的游戏场景,W移动的是游戏对象 ps2:三指 = 左键拖动

  9. 路飞学城Python-Day29(第四模块-并发编程)

    01-进程与程序的概念 并发:多进程和多线程 进程的概念:进程就是正在执行的过程,一个应用程序不是进程,只有应用程序启动以后才能说是进程,进程是一个抽象的概念,起源于操作系统 02-操作系统介绍 应用 ...

  10. 《Exception》第八次团队作业:Alpha冲刺(大结局)

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件测试基础技术.2.学习迭代式增量软 ...