1305: [cqoi2009] dance dancing time limit: 5 sec memory limit: 162 MB
Submit: 1340 solved: 581
[Submit] [Status] Description
There were N boys and N girls at a dance. At the beginning of each song, all boys and girls dance in a ballroom dance. Every boy won't dance two (or more) dance songs with the same girl. Some boys and girls like each other, while others do not (not "one-way "). Each boy can only dance with K girls who do not like it, and each girl can only dance with K boys who do not like it. Provide information about whether boys and girls like each other. How many dance songs can they have at most?
Input
The first line contains two integers, N and K. The following n lines contain N characters in each row. The J character of line I is 'y' and only applies to boys I and girls J.
Output
Only one number, that is, the maximum number of dance music.
Sample input3 0
Yyy
Yyy
Yyy
Sample output3hint
N <= 50 k <= 30
Source
Enhanced Data by dwellings and liyizhen2
This topic is known as a complete water problem after a binary + online stream.
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<ctime>#include<cmath>#include<algorithm>#include<set>#include<map>#include<vector>#include<string>#include<queue>using namespace std;#ifdef WIN32#define LL "%I64d"#else#define LL "%lld"#endif#define MAXN 55#define MAXV MAXN*MAXN*2#define MAXE MAXV*2#define INF 0x3f3f3f3f#define INFL 0x3f3f3f3f3f3f3f3fLLtypedef long long qword;inline int nextInt(){ char ch; int x=0; bool flag=false; do ch=getchar(),flag=(ch==‘-‘)?true:flag; while(ch<‘0‘||ch>‘9‘); do x=x*10+ch-‘0‘; while (ch=getchar(),ch<=‘9‘ && ch>=‘0‘); return x*(flag?-1:1);}int n,m;char mp[MAXN][MAXN];struct Edge{ int val,np; Edge *next,*neg;}E[MAXE],*V[MAXV];int tope=1;int sour=0,sink=1;inline void addedge(int x,int y,int z){ //cout<<"Add edge:<"<<tope+1<<">"<<x<<" "<<y<<":"<<z<<endl; E[++tope].np=y; E[tope].val=z; E[tope].next=V[x]; V[x]=&E[tope]; E[++tope].np=x; E[tope].val=0; E[tope].next=V[y]; V[y]=&E[tope]; E[tope].neg=&E[tope-1]; E[tope-1].neg=&E[tope];}int q[MAXV],lev[MAXV];int vis[MAXV],bfstime=0;bool bfs(){ int i,j; int head=-1,tail=0; Edge *ne; lev[sour]=1; vis[sour]=++bfstime; q[0]=sour; while (head<tail) { for (ne=V[q[++head]];ne;ne=ne->next) { if (!ne->val || vis[ne->np]==bfstime)continue; q[++tail]=ne->np; vis[ne->np]=bfstime; lev[ne->np]=lev[q[head]]+1; } } return vis[sink]==bfstime;}int dfs(int now,int maxf){ int ret=0,t; if (now==sink || !maxf)return maxf; Edge* ne; for (ne=V[now];ne;ne=ne->next) { if (!ne->val || lev[ne->np]!=lev[now]+1)continue; t=dfs(ne->np,min(maxf,ne->val)); ne->val-=t; ne->neg->val+=t; maxf-=t; ret+=t; //cout<<"Flow:"<<now<<"-"<<ne->np<<":"<<x<<"("<<ne->val<<")"<<endl; } if (!ret)lev[now]=-1; return ret;}int dinic(){ int ret=0; while (bfs()) { ret+=dfs(sour,INF); } return ret;}int main(){ freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int i,j,k; int x,y,z; scanf("%d%d\n",&n,&m); for (i=0;i<n;i++) { fgets(mp[i],MAXN,stdin); } int ans=0; int l=0,r=n+1,mid; while (l+1<r) { memset(V,0,sizeof(V)); tope=-1; mid=(l+r)>>1; for (i=0;i<n;i++) { addedge(sour,2+i,mid); addedge(2+i,2+i+n*2,m); addedge(2+i+n,sink,mid); addedge(2+i+n+n*2,2+i+n,m); } for (i=0;i<n;i++) { for (j=0;j<n;j++) { if (mp[i][j]!=‘Y‘) { addedge(2+i+n*2,2+n+j+n*2,1); }else { addedge(2+i,2+j+n,1); } } } ans=dinic(); if (ans!=mid*n) { r=mid; }else { l=mid; } } printf("%d\n",l); return 0;}
Bzoj 1305: [cqoi2009] Dance binary + website stream Determination