【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的 ...
随机推荐
- 基于HttpClient实现网络爬虫~以百度新闻为例
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/40891791 基于HttpClient4.5实现网络爬虫请訪问这里:http:/ ...
- vue+mousemove实现拖动,鼠标移动过快拖动就失效
今天用vue+原生js的mousemove事件,写了个拖动,发现只能慢慢拖动才行,鼠标只要移动快了,就失效,不能拖动了: 搞了半天在,总算解决了,但是问题的深层原理还没搞清楚,知道的大侠可以留言分享, ...
- 谷歌浏览器console.log()失效,打印不出来内容
这个问题困扰好几天了,网上说的都说的是下图: 勾选这三个就好了,但是我的本来就是勾选上的,还是不行. 后来发现这个: 把这个去掉就可以了,如下图: 原来是因为之前调试js的时候,使用了这个过滤,导致对 ...
- python tensorflow 学习
Tensorflow系列——Saver的用法:http://blog.csdn.net/u011500062/article/details/51728830 Tensorflow学习系列(二): t ...
- linux下nginx php配置redis
之前一直遇到,Module compiled with module API=20090626这个坑问题!!! NOTICE: PHP message: PHP Warning: PHP Star ...
- 忘记glassfish密码,那就重置密码呗
方法一:如果现有的 domain 上并没有你所需要的东西,删除现有的 domain,重新创建一个 domain. 找到安装glassfish的目录下的 \bin\asadmin 目录,然后打开asad ...
- 【BZOJ4177】Mike的农场 最小割
[BZOJ4177]Mike的农场 Description Mike有一个农场,这个农场n个牲畜围栏,现在他想在每个牲畜围栏中养一只动物,每只动物可以是牛或羊,并且每个牲畜围栏中的饲养条件都不同,其中 ...
- Zookeeper数据与存储
一.前言 前面分析了Zookeeper对请求的处理,本篇博文接着分析Zookeeper中如何对底层数据进行存储,数据存储被分为内存数据存储于磁盘数据存储. 二.数据与存储 2.1 内存数据 Zooke ...
- nginx日志自动切分
#!/bin/bash NGINX_LOG_PATH=/data/nginx-/logs # 昨天 YESTERDAY=$(date -d "yesterday" +%Y-%m-% ...
- iOS 运行时详解
注:本篇文章转自:http://www.jianshu.com/p/adf0d566c887 一.运行时简介 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行 ...