【Codeforces】Gym 101173B Bipartite Blanket 霍尔定理+状压DP
题意
给一张$n\times m$二分图,带点权,问有多少完美匹配子集满足权值和大于等于$t$
这里有一个结论:对于二分图$\mathbb{A}$和$\mathbb{B}$集合,如果子集$A \in \mathbb{A},B \in \mathbb{B}$,且$A,B$分别是完美匹配的子集,那么$A \cup B$属于一个完美匹配
有了这个结论之后,考虑单侧,枚举子集$S$,利用霍尔定理判定$S$是否是完美匹配,并通过dp转移状态,记录下单侧所有满足条件的权值和,然后两侧一起考虑累加得到答案
时间复杂度$O((n+m)2^{max(n,m)})$
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1 << 20;
int n, m, a[N + 5], b[N + 5], cnt[N + 5], L[N + 5], R[N + 5], fl[N + 5], fr[N + 5], t;
char str[100][100];
vector<int> g1, g2;
int main() {
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i) scanf("%s", str[i]);
for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
for(int i = 0; i < m; ++i) scanf("%d", &b[i]);
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(str[i][j] == '1') {
L[i] |= (1 << j); R[j] |= (1 << i);
}
}
}
scanf("%d", &t);
for(int i = 0; i <= max((1 << n), (1 << m)); ++i) cnt[i] = cnt[i>>1] + (i & 1);
for(int s = 0; s < (1 << n); ++s) {
int now = 0, v = 0;
fl[s] = 1;
for(int i = 0; i < n; ++i) {
if((s >> i) & 1) {
v += a[i]; now |= L[i];
fl[s] &= fl[s ^ (1 << i)];
}
}
if(fl[s] && cnt[s] <= cnt[now]) g1.push_back(v);
else fl[s] = 0;
}
for(int s = 0; s < (1 << m); ++s) {
int now = 0, v = 0;
fr[s] = 1;
for(int i = 0; i < m; ++i) {
if((s >> i) & 1) {
v += b[i]; now |= R[i];
fr[s] &= fr[s ^ (1 << i)];
}
}
if(fr[s] && cnt[s] <= cnt[now]) g2.push_back(v);
else fr[s] = 0;
}
sort(g1.begin(), g1.end());
LL ans = 0;
for(int i = 0; i < g2.size(); ++i) {
ans += g1.size() - (lower_bound(g1.begin(), g1.end(), t - g2[i]) - g1.begin());
}
cout << ans << endl;
return 0;
}
/*
3 3
010
111
010
1 2 3
8 5 13
21
*/
/*
3 2
01
11
10
1 2 3
4 5
8
*/
【Codeforces】Gym 101173B Bipartite Blanket 霍尔定理+状压DP的更多相关文章
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- Educational Codeforces Round 13 E. Another Sith Tournament 状压dp
E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...
- Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...
- 『Exclusive Access 2 dilworth定理 状压dp』
Exclusive Access 2 Description 给出 N 个点M 条边的无向图,定向得到有向无环图,使得最长路最短. N ≤ 15, M ≤ 100 Input Format 第一行一个 ...
- CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP
题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...
- bzoj4903 & loj2264 [Ctsc2017]吉夫特 Lucas 定理+状压DP
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4903 https://loj.ac/problem/2264 http://uoj.ac/pr ...
- Codeforces 279D The Minimum Number of Variables 状压dp
The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...
- codeforces#580 D. Kefa and Dishes(状压dp)
题意:有n个菜,每个菜有个兴奋值,并且如果吃饭第i个菜立即吃第j个菜,那么兴奋值加ma[i][j],求吃m个菜的最大兴奋值,(n<=18) 分析:定义dp[status][last],statu ...
- Codeforces Round #585 (Div. 2) E. Marbles(状压dp)
题意:给你一个长度为n的序列 问你需要多少次两两交换 可以让相同的数字在一个区间段 思路:我们可以预处理一个数组cnt[i][j]表示把i放到j前面需要交换多少次 然后二进制枚举后 每次选择一个为1的 ...
随机推荐
- js 判断 IE 浏览器
遇到一些IE兼容问题,可以考虑在该浏览器环境下,用js控制样式,以下是判断IE版本的js代码 var browser=navigator.appName var b_version=navigator ...
- 如何搭建maven项目和搭建ssm框架
1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One ...
- 嵌入式开发之davinci--- 8148/8168/8127 中的大屏分布式拼接显示系统
其实在接触从12年接触8127系列ipnc 时看到200w和500w的高清像素,我就萌生了视频拼接的兴趣,没想到今年的安博会就有公司推出产品了,它就是上海的环视科技,从他的主页可以看到,明显的有个只能 ...
- WEBserver、应用程序server、HTTPserver差别
WEBserver.应用程序server.HTTPserver差别 WEBserver.应用程序server.HTTPserver有何差别?IIS.Apache.Tomcat.Weblogic.Web ...
- Unity合并选中物体的Mesh
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; pu ...
- tomcat下发布项目,遇到的问题总结
以前一直是在eclipse下启动tomcat,然后访问web项目.今天脑门一热,就想用tomcat的bin目录下的startup.bat来启动tomcat,虽然tomcat的启动很顺利,但是访问网页的 ...
- idangerous swiper
最近使用Swipe.js,发现中文的资料很少,试着翻译了一下.能力有限,翻译难免错漏,欢迎指出,多谢! 翻译自:http://www.idangero.us/sliders/swiper/api.ph ...
- 让Xcode支持高版本系统设备真机测试
最新支持11.2 (15C107) Xcode只可以支持iPhone手机对应iOS系统以下的真机测试.一般想要支持最新的iPhone手机系统,有两个方法. 第一.就需要更新Xcode,这一个方法有一个 ...
- lua2json
lua对象json序列化,很简单,没做中文的unicode编码了 local function bool2json(v) return v and "true" or " ...
- 洛谷 2216 [HAOI2007]理想的正方形
题目戳这里 一句话题意 给你一个a×b的矩形,求一个n×n的子矩阵,矩阵里面的最大值和最小值之差最小. Solution 这个题目许多大佬都是单调队列,但是我不是很会,只好用了比较傻逼的方法: 首先我 ...