题目链接
bzoj 1934: [Shoi2007]Vote 善意的投票
题解
睡觉作为源点,不睡作为汇点
对于一个人违背自己的意愿,连向与自己意愿相反的源\汇,容量为1
对于朋友意见相反,在朋友之间连容量为2的双向边,切得时候双向边使得该边漫流
求一边最小割就是答案
代码
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10007;
#define INF 0x7fffffff
using std::queue;
inline int read() {
int x=0;
char c=getchar();
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0')x=x*10+c-'0',c=getchar();
return x;
}
int head[maxn];
int n,m,num=0,S,T;
int bel[maxn];
struct node{
int v,next,flow;
}edge[maxn<<4];
void add_edge(int u,int v,int flow) {
edge[++num].v=v;edge[num].flow=flow;edge[num].next=head[u];head[u]=num;
edge[++num].v=u;edge[num].flow=0;edge[num].next=head[v];head[v]=num;
}
int cur[maxn],lev[maxn];
bool bfs() {
memset(lev,-1,sizeof lev);
memcpy(cur,head,sizeof head);
queue<int>que;
que.push(S);lev[S]=0;
while(!que.empty()) {
int u=que.front();que.pop();
for(int i=head[u];i;i=edge[i].next) {
int v=edge[i].v;
if(edge[i].flow>0&&lev[v]<0) {
lev[v]=lev[u]+1;que.push(v);
}
}
}
if(lev[T]!=-1)return true;
else return false;
}
int dfs(int now,int flow) {
if(now==T)return flow;
int rest=0,delta;
for(int &i=cur[now];i;i=edge[i].next) {
int v=edge[i].v;
if(lev[v]==lev[now]+1&&edge[i].flow>0) {
delta=dfs(v,min(flow-rest,edge[i].flow));
if(delta) {
edge[i].flow-=delta;
edge[i^1].flow+=delta;
rest+=delta;if(rest==flow)break;
}
}
}
if(rest==flow)lev[now]=-1;
return rest;
}
int dinic() {
int ans=0;
while(bfs())
ans+=dfs(S,INF);
return ans;
}
int main() {
n=read(),m=read();
T=n+1;
for(int p,i=1;i<=n;++i) {
p=read();
if(p==1)add_edge(S,i,1),bel[i];
else add_edge(i,T,1);
}
for(int a,b,i=1;i<=m;++i) {
a=read(),b=read();
add_edge(a,b,2);
add_edge(b,a,2);
}
printf("%d\n",dinic());
return 0;
}