分析
这道题挺巧妙的
看题干是一个裸的二分图匹配,但问题是点数已经到达了
1
0
6
10 ^ 6
106的规模,众所周知
d
i
n
i
c
dinic
dinic算法只能处理
1
0
4
∼
1
0
5
10 ^ 4 \sim 10^ 5
104∼105之内的网络,所以我们得想办法去优化这个网络
观察一下可以发现,左边的点数有
1
0
5
10 ^ 5
105个,右边的点数有
10
10
10个,右边每一个点都对应着
2
10
(
1024
)
2^{10}(1024)
210(1024)种状态,所以,我们可以优化建图方式
源点和每一种状态相连,流量为每一种状态出现的次数,每一种状态和可以到达的星球相连,流量为为一种状态出现的次数,每一个星球和汇点相连,流量为每一个星球的最大容纳数量,跑
d
i
n
i
c
dinic
dinic即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10,M = N * 2;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){return (b>0)?gcd(b,a%b):a;}
int num[2000];
int n,m,S,T;
int h[N],ne[M],e[M],f[M],idx;
int q[N], d[N], cur[N];
void add(int x,int y,int z){
e[idx] = y,ne[idx] = h[x],f[idx] = z,h[x] = idx++;
e[idx] = x,ne[idx] = h[y],f[idx] = 0,h[y] = idx++;
}
bool bfs(){
int hh = 0, tt = 0;
memset(d,-1,sizeof d);
q[0] = S, d[S] = 0, cur[S] = h[S];
while(hh <= tt){
int t = q[hh ++ ];
for(int i = h[t];~i;i = ne[i]){
int j = e[i];
if(d[j] == -1 && f[i]){
d[j] = d[t] + 1;
cur[j] = h[j];
if(j == T) return true;
q[ ++ tt] = j;
}
}
}
return false;
}
int find(int u,int limit){
if(u == T) return limit;
int flow = 0;
for(int i = cur[u];~i && flow < limit;i = ne[i]){
cur[u] = i;
int j = e[i];
if(d[j] == d[u] + 1 && f[i]){
int t = find(j,min(f[i],limit - flow));
if(!t) d[j] = -1;
f[i] -= t,f[i ^ 1] +=t,flow += t;
}
}
return flow;
}
int dinic(){
int res = 0,flow;
while(bfs()) while(flow = find(S,INF)) res += flow;
return res;
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(num,0,sizeof num);
memset(h,-1,sizeof h);
idx = 0;
for(int i = 1;i <= n;i++){
int res = 0;
for(int j = 1;j <= m;j++){
res <<= 1;
int x;
read(x);
if(x) res++;
}
// cout << res << endl;
num[res]++;
}
S = 0,T = N - 10;
for(int i = 1;i < 2000;i++) {
if(!num[i]) continue;
add(S,i,num[i]);
for(int j = 0;j < m;j++)
if((i >> j) & 1) {
// cout << i << ' ' << m << endl;
add(i,2000 + j,num[i]);
}
}
for(int i = 0;i < m;i++){
int x;
read(x);
add(2000 + i,T,x);
}
int res = dinic();
if(res == n) puts("YES");
else puts("NO");
}
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/