简单建图会T,得状态压缩简化图,提交时用C++....不然也会T
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 1000000000
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
int n,m;
const int mm=2222*20;
const int mn=2222;
int node,s,t,edge;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
void init(int _node,int _s,int _t)
{
node=_node, s=_s, t=_t;
for(int i=0;i<node;++i)
head[i]=-1;
edge=0;
}
void addedge(int u,int v,int c)
{
ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
int i,u,v,l,r=0;
for(i=0;i<node;++i) dis[i]=-1;
dis[ q[r++]=s ] = 0;
for(l=0;l<r;l++)
{
for(i=head[ u=q[l] ]; ~i ;i=next[i])
if(flow[i] && dis[ v=ver[i] ]<0)
{
dis[ q[r++]=v ]=dis[u]+1;
if(v==t) return 1;
}
}
return 0;
}
int Dinic_dfs(int u,int exp)
{
if(u==t) return exp;
for(int &i=work[u],v,temp; ~i ;i=next[i])
{
if(flow[i] && dis[ v=ver[i] ]==dis[u]+1 && ( temp=Dinic_dfs(v,min(exp,flow[i])) )>0)
{
flow[i]-=temp;
flow[i^1]+=temp;
return temp;
}
}
return 0;
}
int Dinic_flow()
{
int ans=0,res,i;
while(Dinic_bfs())
{
for(i=0;i<node;++i) work[i]=head[i];
while( res=Dinic_dfs(s,INF) ) ans+=res;
}
return ans;
}
int vis[2222];
int main()
{
while(~scanf("%d%d",&n,&m))
{
int a;
CLR(vis);
FOR(i,1,n)
{
int s=0;
REP(j,m)
{
scanf("%d",&a);
if(a)
s|=(a<<j);
}
vis[s]++;
}
int nn=n;
n= 1<<m ;
init(n+m+1,0,n+m);
FOR(i,1,n-1)
{
if(vis[i])
{
addedge(s,i,vis[i]);
REP(j,m)
{
if( i & (1<<j) )
addedge(i,n+j,INF);
}
}
}
REP(i,m)
{
scanf("%d",&a);
if(a)
addedge(n+i,t,a);
}
int ans=Dinic_flow();
//cout<<ans<<endl;
if(ans == nn)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}