在第一个例子中,所有小朋友都投赞成票就能得到最优解
Day2
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #define N 3000 #define inf 1000000000 using namespace std; int n,m; int point[N],next[N],belong[N],tot=-1,u[N],remain[N]; int p[N],last[N],cnt,v[N],maxn,cnt1[N],num1[N],deep[N],laste[N],cur[N]; void add(int x,int y,int z) { tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z; tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; } int addflow(int s,int t) { int now=t; int ans=inf; while(now!=s) { ans=min(ans,remain[last[now]]); now=v[last[now]^1]; } now=t; while(now!=s) { remain[last[now]]-=ans; remain[last[now]^1]+=ans; now=v[last[now]^1]; } return ans; } void bfs(int s,int t) { for (int i=s;i<=t;i++) deep[i]=t; deep[t]=0; queue<int> p; p.push(t); while (!p.empty()) { int now=p.front(); p.pop(); for (int i=point[now];i!=-1;i=next[i]) if (deep[v[i]]==t&&remain[i^1]) deep[v[i]]=deep[now]+1,p.push(v[i]); } } int isap(int s,int t) { int maxflow=0; bfs(s,t); for (int i=s;i<=t;i++) cur[i]=point[i]; for (int i=s;i<=t;i++) num1[deep[i]]++; int now=s; while (deep[s]<t) { if (now==t) { maxflow+=addflow(s,t); now=s; } int p=false; for (int i=cur[now];i!=-1;i=next[i]) if (deep[v[i]]+1==deep[now]&&remain[i]) { cur[now]=i; p=true; last[v[i]]=i; now=v[i]; break; } if (!p) { int minn=t-1; for (int i=point[now];i!=-1;i=next[i]) if (remain[i]) minn=min(minn,deep[v[i]]); if (!--num1[deep[now]]) break; deep[now]=minn+1; num1[deep[now]]++; cur[now]=point[now]; if (now!=s) now=v[last[now]^1]; } } return maxflow; } int main() { freopen("a.in","r",stdin); scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%d",&belong[i]); int num=2*n+2; tot=-1; memset(next,-1,sizeof(next)); memset(point,-1,sizeof(point)); for (int i=1;i<=n;i++) { if (!belong[i]) add(1,i+1,1); else add(i+n+1,num,1); } for (int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); if (belong[x]==belong[y]) continue; if (!belong[x]) add(x+1,y+n+1,1); else add(y+1,x+n+1,1); } printf("%d\n",isap(1,num)); }