首页 > 代码库 > bzoj2744: [HEOI2012]朋友圈

bzoj2744: [HEOI2012]朋友圈

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int nn;
#define in(x) scanf("%d",&x)
#define in2(x,y) scanf("%d%d",&x,&y)
#define in3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define N 3010
#define F(i,x) for(int i=1;i<=x;i++)
int a[N],b[N],m,n1,n2,k1[210],k2[210];
int A,B;
int e[N],ne[N*N/2],v[N*N/2];
int ff[210][N];
int l[N];
int been[N];
int d[N];
int ans;
int num;
void add(int x,int y){
    ne[++nn]=e[x],e[x]=nn,v[nn]=y;
}
bool dfs(int x){
    int y;
    for(int i=e[x];i;i=ne[i]){
        if(!d[y=v[i]]&&num!=been[y]){
            been[y]=num;
            if(!l[y]||dfs(l[y])){
                l[y]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main(){
   in3(A,B,m);
   F(i,A)in(a[i]);
   F(i,B)in(b[i]);
   F(i,A)F(j,B)ff[i][j]=1;
   F(i,m){
          int x,y;
          in2(x,y);
          ff[x][y]=0;
   }
    F(i,A)if(a[i]&1)k1[++n1]=i;
    else k2[++n2]=i;
    F(i,B)F(j,B)if((b[i]&1)&&!(b[j]&1)){
        int ss=0;
        for(int k=0;(1LL<<k)<=(b[i]|b[j]);k++)if((1<<k)&(b[i]|b[j]))ss++;
        if(!(ss&1))add(i,j);
    }
    F(i1,n1+1)F(j1,n2+1){
        int i=k1[i1],j=k2[j1];
        int t=0;
        F(z1,B)if(ff[i][z1]||ff[j][z1])d[z1]=1,l[z1]=0,t++;
        else d[z1]=0,l[z1]=0;
        F(z1,B)if(b[z1]&1){
            if(d[z1])continue;
            num++;
            if(dfs(z1))t++;
        }
        if(i1<=n1)t--;
        if(j1<=n2)t--;
        ans=max(ans,B-t);
    }
    cout<<ans;
}