POJ [P2289] Jamie's Contact Groups
二分+二分图多重匹配
辣鸡ACM式读入
对于这种奇葩的读入方法,还是老老实实的用scanf吧
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define RST(a) memset((a),0,sizeof((a)))
using namespace std;
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return rv*fh;
}
int n,m,g[1005][1005],match[1005][1005],lim,cnt[1005];
bool f[1005];
bool hungarian(int u){
for(int i=1;i<=g[u][0];i++){
int v=g[u][i];
if(!f[v]){
f[v]=1;
if(cnt[v]<lim){
match[v][++cnt[v]]=u;
return 1;
}
for(int j=1;j<=cnt[v];j++){
if(hungarian(match[v][j])){
match[v][j]=u;
return 1;
}
}
}
}
return 0;
}
bool chk(){
/*RST(match);*/RST(cnt);
for(int i=1;i<=n;i++){
RST(f);
if(!hungarian(i)) return 0;
}
return 1;
}
int main(){
while(1){
n=init();m=init();
if(!n&&!m) break;
char s[20];RST(g);
for(int i=1;i<=n;i++){
scanf("%s",s);//cout<<s<<endl;
while(1){
int t;
scanf("%d",&t);
t++;
g[i][++g[i][0]]=t;
if(cin.peek()=='\n'||cin.peek()=='\r'||(cin.peek()>='A'&&cin.peek()<='Z')) break;
}
}
int l=1,r=n;
int ans=0;
while(l<=r){
lim=(l+r)>>1;
if(chk()) ans=lim,r=lim-1;
else l=lim+1;
}
cout<<ans<<endl;
}
return 0;
}