题目传送门
做网络流快疯了。
解法:
好水。。
明显的最小割嘛。。
本来同意的连st容量为1
本来不同意的连ed容量为1。
然后图中的边就互相连嘛。
代码实现:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
struct node {int x,y,c,next,other;}a[1100000];int len,last[1100];
void ins(int x,int y,int c) {
int k1,k2;
len++;k1=len;a[len].x=x;a[len].y=y;a[len].c=c;a[len].next=last[x];last[x]=len;
len++;k2=len;a[len].x=y;a[len].y=x;a[len].c=0;a[len].next=last[y];last[y]=len;
a[k1].other=k2;a[k2].other=k1;
}
int list[1100],head,tail,st,ed,h[1100];
bool bt_h() {
head=1;tail=2;list[1]=st;memset(h,0,sizeof(h));h[st]=1;
while(head!=tail) {
int x=list[head];
for(int k=last[x];k;k=a[k].next) {
int y=a[k].y;
if(h[y]==0&&a[k].c>0) {h[y]=h[x]+1;list[tail++]=y;}
}head++;
}if(h[ed]==0)return false;return true;
}
int findflow(int x,int f) {
if(x==ed)return f;
int s=0,t;
for(int k=last[x];k;k=a[k].next) {
int y=a[k].y;
if(h[y]==h[x]+1&&a[k].c>0&&s<f) {t=findflow(y,min(a[k].c,f-s));s+=t;a[k].c-=t;a[a[k].other].c+=t;}
}if(s==0)h[x]=0;return s;
}const int inf=999999999;
int main() {
int n,m;scanf("%d%d",&n,&m);st=n+1,ed=st+1;len=0;memset(last,0,sizeof(last));
for(int i=1;i<=n;i++) {int x;scanf("%d",&x);if(x==1)ins(st,i,1);else ins(i,ed,1);}
for(int i=1;i<=m;i++) {int x,y;scanf("%d%d",&x,&y);ins(x,y,1);ins(y,x,1);}
int ans=0;while(bt_h()==true)ans+=findflow(st,inf);printf("%d\n",ans);
return 0;
}