【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的 ...
随机推荐
- Python修饰器的函数式编程(转)
From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...
- python#模拟发送电子邮件
#-*- coding:utf-8 -*- #模拟发送电子邮件 from email.mime.text import MIMEText from_addr = 'aa@sss.com' passwo ...
- 有关于__align(n) ,内存对齐
__align __align 关键字指示编译器在 n 字节边界上对齐变量. __align 是一个存储类修饰符.它不影响函数的类型. 语法 __align(n) 其中: n 是对齐边界. 对于局部变 ...
- iptables基础及samba配置举例
iptable基本概念 iptables防火墙包含两部分,即位于用户空间的iptables模块和位于内核空间netfilter模块.用户空间模块提供插入.修改和除去包过滤表中规则,内核模块进行实际的过 ...
- 如何使用subversion管理iOS源代码
本文转载至 http://2009315319.blog.51cto.com/701759/819216 使用subversion管理iOS源代码 1.安装和配置subversion服务器 在wind ...
- 2820: YY的GCD
2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1693 Solved: 901[Submit][Status][Discu ...
- poj2421
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22048 Accepted: 93 ...
- META-INF中的INF的意思
1 META是元的意思,比如meta data,元数据. 2 什么是meta data 元数据就是描述其它数据的数据,比如web page中的meta data,包括关键字,对该网页的描述等等. 3 ...
- images have the “stationarity” property, which implies that features that are useful in one region are also likely to be useful for other regions.
Convolutional networks may include local or global pooling layers[clarification needed], which combi ...
- php数据类型的true和false