现在的位置: 首页 > 综合 > 正文

【bzoj1934】 [Shoi2007]Vote 善意的投票

2018年04月24日 ⁄ 综合 ⁄ 共 992字 ⁄ 字号 评论关闭
#include<iostream>
#include<cstdio>
#include<cstring>
#define inf 0x7fffffff
using namespace std;
struct data{
	int to,next,v;
}e[100001];
int n,m,ans,cnt=1,head[305],h[305],q[305];
void insert(int u,int v,int w){
	e[++cnt].to=v;
	e[cnt].next=head[u];
	e[cnt].v=w;
	head[u]=cnt;
}
bool bfs(){
	int t=0,w=1,i,now;
	memset(h,-1,sizeof(h));
	h[0]=q[0]=0;
	while(t<w){
		now=q[t++];i=head[now];
		while(i){
			if(e[i].v&&h[e[i].to]==-1){
				h[e[i].to]=h[now]+1;
				q[w++]=e[i].to;
			}
			i=e[i].next;
		}
	}
	if(h[n+1]==-1)return 0;
	else return 1;
}
int dfs(int x,int f){
	if(x==n+1)return f;
	int i=head[x],w,used=0;
	while(i){
		if(e[i].v&&h[e[i].to]==h[x]+1){
			w=f-used;
			w=dfs(e[i].to,min(e[i].v,w));
			e[i].v-=w;
			e[i^1].v+=w;
			used+=w;
			if(used==f)return f;
		}
		i=e[i].next;
	}
	if(!used)h[x]=-1;
	return used;
}
void dinic(){
	while(bfs())ans+=dfs(0,inf);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		int t;scanf("%d",&t);
		if(t){
			insert(0,i,1);
			insert(i,0,0);
		}
		else{
			insert(i,n+1,1);
			insert(n+1,i,0);
		}
	}
	for(int i=1;i<=m;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		insert(a,b,1);
		insert(b,a,1);
	}
	dinic();
	printf("%d",ans);
	return 0;
}

抱歉!评论已关闭.