BZOJ 2744

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=4000;
const int oo=1000000000;
int n,m,c;
int ans=0;
int g[1001][1001];
int gf[1001][1001];
int a[maxn],b[maxn];

struct Edge{
    int from,to,cap,flow;
};
vector<int>G[maxn];
vector<Edge>edges;
void Addedge(int x,int y,int z){
    Edge e;
    e.from=x;e.to=y;e.cap=z;e.flow=0;
    edges.push_back(e);
    e.from=y;e.to=x;e.cap=0;e.flow=0;
    edges.push_back(e);
    int cc=edges.size();
    G[x].push_back(cc-2);
    G[y].push_back(cc-1);
}

int s,t;
int vis[maxn];
int d[maxn];
queue<int>q;
int Bfs(){
    memset(vis,0,sizeof(vis));
    vis[s]=1;d[s]=0;q.push(s);
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=0;i<G[x].size();++i){
            Edge e=edges[G[x][i]];
            if((!vis[e.to])&&(e.cap>e.flow)){
                d[e.to]=d[x]+1;
                vis[e.to]=1;
                q.push(e.to);
            }
        }
    }
    return vis[t];
}

int cur[maxn];
int Dfs(int x,int a){
    if((x==t)||(a==0))return a;
    
    int nowflow=0,f=0;
    for(int i=cur[x];i<G[x].size();++i){
        cur[x]=i;
        Edge e=edges[G[x][i]];
        if((d[x]+1==d[e.to])&&((f=Dfs(e.to,min(a,e.cap-e.flow)))>0)){
            nowflow+=f;
            a-=f;
            edges[G[x][i]].flow+=f;
            edges[G[x][i]^1].flow-=f;
            if(a==0)break;
        }
    }
    return nowflow;
}

int Maxflow(){
    int flow=0;
    while(Bfs()){
        memset(cur,0,sizeof(cur));
        flow+=Dfs(s,oo);
    }
    return flow;
}

void Dinicinit(){
}

int Getsize(int x){
    int ret=0;
    while(x){
        ++ret;
        x^=(x&(-x));
    }
    return ret;
}

int main(){
    scanf("%d%d%d",&n,&m,&c);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    for(int i=1;i<=m;++i)scanf("%d",&b[i]);
    while(c--){
        int x,y;
        scanf("%d%d",&x,&y);
        g[x][y]=1;
    }
    for(int i=1;i<=m;++i){
        for(int j=1;j<=m;++j){
            if(i==j)continue;
            if(Getsize(b[i]|b[j])%2)g[i][j]=1;
        }
    }
    
    s=n+m+1;t=s+1;
    for(int i=1;i<=n;++i){
        for(int j=i;j<=n;++j){
            if(!((a[i]^a[j])&1))continue;
            Dinicinit();
            for(int k=1;k<=m;++k){
                if((!g[i][k])||(!g[j][k]))continue;
                if(k%2)Addedge(s,k,1);
                else Addedge(k,t,1);
            }
            for(int x=1;x<=m;++x){
                if(b[x]%2==0)continue;
                for(int y=1;y<=m;++y){
                    if(b[x]%2)continue;
                    if(!g[x][y])Addedge(x,y,1);
                }
            }
            int flow=Maxflow();
            ans=max(ans,m-flow+2-(i==j));
        }
    }
    Dinicinit();
    for(int i=1;i<=m;++i){
        if(b[i]%2)Addedge(s,i,1);
        else Addedge(i,t,1);
    }
    for(int i=1;i<=m;++i){
        if(b[i]%2==0)continue;
        for(int j=1;j<=m;++j){
            if(b[j]%2)continue;
            if(!g[i][j])Addedge(i,j,1);
        }
    }
    ans=max(ans,m-Maxflow());
    cout<<ans<<endl;
    return 0;
}

 

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int maxn=4000;const int oo=1000000000;int n,m,c;int ans=0;int g[1001][1001];int gf[1001][1001];int a[maxn],b[maxn];
struct Edge{int from,to,cap,flow;};vector<int>G[maxn];vector<Edge>edges;void Addedge(int x,int y,int z){Edge e;e.from=x;e.to=y;e.cap=z;e.flow=0;edges.push_back(e);e.from=y;e.to=x;e.cap=0;e.flow=0;edges.push_back(e);int cc=edges.size();G[x].push_back(cc-2);G[y].push_back(cc-1);}
int s,t;int vis[maxn];int d[maxn];queue<int>q;int Bfs(){memset(vis,0,sizeof(vis));vis[s]=1;d[s]=0;q.push(s);while(!q.empty()){int x=q.front();q.pop();for(int i=0;i<G[x].size();++i){Edge e=edges[G[x][i]];if((!vis[e.to])&&(e.cap>e.flow)){d[e.to]=d[x]+1;vis[e.to]=1;q.push(e.to);}}}return vis[t];}
int cur[maxn];int Dfs(int x,int a){if((x==t)||(a==0))return a;int nowflow=0,f=0;for(int i=cur[x];i<G[x].size();++i){cur[x]=i;Edge e=edges[G[x][i]];if((d[x]+1==d[e.to])&&((f=Dfs(e.to,min(a,e.cap-e.flow)))>0)){nowflow+=f;a-=f;edges[G[x][i]].flow+=f;edges[G[x][i]^1].flow-=f;if(a==0)break;}}return nowflow;}
int Maxflow(){int flow=0;while(Bfs()){memset(cur,0,sizeof(cur));flow+=Dfs(s,oo);}return flow;}
void Dinicinit(){}
int Getsize(int x){int ret=0;while(x){++ret;x^=(x&(-x));}return ret;}
int main(){scanf("%d%d%d",&n,&m,&c);for(int i=1;i<=n;++i)scanf("%d",&a[i]);for(int i=1;i<=m;++i)scanf("%d",&b[i]);while(c--){int x,y;scanf("%d%d",&x,&y);g[x][y]=1;}for(int i=1;i<=m;++i){for(int j=1;j<=m;++j){if(i==j)continue;if(Getsize(b[i]|b[j])%2)g[i][j]=1;}}s=n+m+1;t=s+1;for(int i=1;i<=n;++i){for(int j=i;j<=n;++j){if(!((a[i]^a[j])&1))continue;Dinicinit();for(int k=1;k<=m;++k){if((!g[i][k])||(!g[j][k]))continue;if(k%2)Addedge(s,k,1);else Addedge(k,t,1);}for(int x=1;x<=m;++x){if(b[x]%2==0)continue;for(int y=1;y<=m;++y){if(b[x]%2)continue;if(!g[x][y])Addedge(x,y,1);}}int flow=Maxflow();ans=max(ans,m-flow+2-(i==j));}}Dinicinit();for(int i=1;i<=m;++i){if(b[i]%2)Addedge(s,i,1);else Addedge(i,t,1);}for(int i=1;i<=m;++i){if(b[i]%2==0)continue;for(int j=1;j<=m;++j){if(b[j]%2)continue;if(!g[i][j])Addedge(i,j,1);}}ans=max(ans,m-Maxflow());cout<<ans<<endl;return 0;}

posted @ 2018-03-30 22:31  ws_zzy  阅读(138)  评论(0编辑  收藏  举报