bzoj1934: [Shoi2007]Vote 善意的投票(最小割)

传送门

 

考虑源点为同意,汇点为反对,那么只要源点向同意的连边,不同意的向汇点连边,求个最小割就是答案

然后考虑朋友之间怎么办,我们令朋友之间连双向边。这样不管怎么割都能对应一种选择情况。那么还是求一个最小割就行了

 1 //minamoto
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 #define inf 0x3f3f3f3f
 7 using namespace std;
 8 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
 9 char buf[1<<21],*p1=buf,*p2=buf;
10 inline int read(){
11     #define num ch-'0'
12     char ch;bool flag=0;int res;
13     while(!isdigit(ch=getc()))
14     (ch=='-')&&(flag=true);
15     for(res=num;isdigit(ch=getc());res=res*10+num);
16     (flag)&&(res=-res);
17     #undef num
18     return res;
19 }
20 const int N=5005,M=500005;
21 int head[N],Next[M],ver[M],edge[M],tot=1;
22 int S,T,dep[N],cur[N],n,m;
23 queue<int> q;
24 inline void add(int u,int v,int e){
25     ver[++tot]=v,Next[tot]=head[u],head[u]=tot,edge[tot]=e;
26     ver[++tot]=u,Next[tot]=head[v],head[v]=tot,edge[tot]=0;
27 }
28 bool bfs(){
29     memset(dep,-1,sizeof(dep));
30     while(!q.empty()) q.pop();
31     for(int i=S;i<=T;++i) cur[i]=head[i];
32     q.push(S),dep[S]=0;
33     while(!q.empty()){
34         int u=q.front();q.pop();
35         for(int i=head[u];i;i=Next[i]){
36             int v=ver[i];
37             if(dep[v]<0&&edge[i]){
38                 dep[v]=dep[u]+1,q.push(v);
39                 if(v==T) return true;
40             }
41         }
42     }
43     return false;
44 }
45 int dfs(int u,int limit){
46     if(u==T||!limit) return limit;
47     int flow=0,f;
48     for(int i=cur[u];i;cur[u]=i=Next[i]){
49         int v=ver[i];
50         if(dep[v]==dep[u]+1&&(f=dfs(v,min(limit,edge[i])))){
51             flow+=f,limit-=f;
52             edge[i]-=f,edge[i^1]+=f;
53             if(!limit) break;
54         }
55     }
56     if(!flow) dep[u]=-1;
57     return flow;
58 }
59 int dinic(){
60     int flow=0;
61     while(bfs()) flow+=dfs(S,inf);
62     return flow;
63 }
64 int main(){
65     //freopen("testdata.in","r",stdin);
66     n=read(),m=read(),S=0,T=n+1;
67     for(int i=1,k;i<=n;++i) k=read(),k&1?add(S,i,1):add(i,T,1);
68     for(int i=1;i<=m;++i){
69         int u=read(),v=read();add(u,v,1),add(v,u,1);
70     }
71     printf("%d\n",dinic());
72     return 0;
73 }

 

posted @ 2018-09-03 17:50  bztMinamoto  阅读(79)  评论(0编辑  收藏
Live2D