# [BZOJ1934/Luogu2057][SHOI2007]Vote 善意的投票 题解

BZOJ1934

Luogu2057

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>

int n,m,St,Ed;
int Head[305],Next[200005],To[200005],Val[200005],En=1;
int Dep[305];

inline void Add(int x,int y)
{
Next[++En]=Head[x],To[Head[x]=En]=y,Val[En]=1;
Next[++En]=Head[y],To[Head[y]=En]=x,Val[En]=0;
}

bool BFS()
{
std::queue<int> q;
memset(Dep,0,sizeof Dep);
Dep[St]=1,q.push(St);
while(!q.empty())
{
int x=q.front(),y;
q.pop();
for(int i=Head[x];i;i=Next[i])
if(Val[i]&&!Dep[y=To[i]])
{
Dep[y]=Dep[x]+1;
if(y==Ed)return true;
q.push(y);
}
}
return false;
}

int Dinic(int x,int Flow)
{
if(x==Ed)return Flow;
int k,Rest=Flow,y;
for(int i=Head[x];i&&Rest;i=Next[i])
if(Val[i]&&Dep[y=To[i]]==Dep[x]+1)
{
k=Dinic(y,std::min(Rest,Val[i]));
if(!k)Dep[y]=0;
Val[i]-=k,Val[i^1]+=k,Rest-=k;
}
return Flow-Rest;
}

int main()
{
scanf("%d%d",&n,&m);
St=n+1,Ed=n+2;
for(int i=1,Sup;i<=n;++i)
{
scanf("%d",&Sup);
if(Sup)Add(St,i);
else Add(i,Ed);
}
for(int x,y;m--;)
{
scanf("%d%d",&x,&y);
Add(x,y),Add(y,x);
}
int Maxf=0,Fs;
while(BFS())
while((Fs=Dinic(St,1<<30)))
Maxf+=Fs;
printf("%d\n",Maxf);
return 0;
}

posted @ 2018-12-19 15:09  LanrTabe  阅读(60)  评论(0编辑  收藏