在第一个例子中,所有小朋友都投赞成票就能得到最优解
Day2
想到是最小割就好办了呢。。。。(喂我没想到啊混蛋)
S向赞成的小孩连,T向反对的连,好友互相连,容量皆为1
OK(根据最小割定义,最小割切的都是从S向T的有向边【话外:反向呢?答:无视】,显然成立)
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (300+2+10) #define MAXM (MAXN*MAXN*4) long long mul(long long a,long long b){return (a*b)%F;} long long add(long long a,long long b){return (a+b)%F;} long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;} int edge[MAXM],pre[MAXN]={0},next[MAXM],weight[MAXM],size=1; void addedge(int u,int v,int w) { edge[++size]=v; weight[size]=w; next[size]=pre[u]; pre[u]=size; } void addedge2(int u,int v,int w){addedge(u,v,w),addedge(v,u,0);} int d[MAXN]={0},cnt[MAXN]={0},s,t; int totflow=0; int sap(int x,int flow) { if (x==t) return flow; int nowflow=0; Forp(x) { int &v=edge[p]; if (d[v]==d[x]-1&&weight[p]>0) { int fl=sap(v,min(flow,weight[p])); weight[p]-=fl,weight[p^1]+=fl,flow-=fl,nowflow+=fl; if (!flow) return nowflow; } } if (!(--cnt[d[x]++])) d[s]=t+1; cnt[d[x]]++; return nowflow; } int n,m,a[MAXN]; int main() { // freopen("conflict.in","r",stdin); // freopen("conflict.out","w",stdout); scanf("%d%d",&n,&m); s=n+1,t=s+1; For(i,n) { scanf("%d",&a[i]); if (a[i]) addedge2(s,i,1); else addedge2(i,t,1); } For(i,m) { int u,v; scanf("%d%d",&u,&v); addedge(u,v,1);addedge(v,u,1); } cnt[0]=n+2; while (d[s]<=t) totflow+=sap(s,INF); cout<<totflow<<endl; return 0; }