Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
AC110 二分+多重匹配水过#include<cstring> #include<cstdio> #include<vector> const int maxn = 1e3+10; using std::vector; vector<int>mp[maxn]; bool vis[maxn]; int cnt[maxn],maxsize[maxn],ylink[maxn][maxn]; int n,m,mid; bool dfs(int u){ int len = mp[u].size(); for(int j=0;j<len;j++){ int i = mp[u][j]; if(!vis[i]){ vis[i]=true; if(cnt[i]<mid){ ylink[i][++cnt[i]]=u; return true; }else { for(int k=1;k<=cnt[i];k++){ if(dfs(ylink[i][k])){ ylink[i][k]=u; return true; } } } } } return false; } bool sove(){ int flag=1; memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++){ memset(vis,false,sizeof(vis)); if(!dfs(i)){flag=0;break;} } if(flag)return true; else return false; } int main() { while(~scanf("%d%d",&n,&m),n+m){ memset(mp,0,sizeof(mp)); char str[10000]; for(int i=1;i<=n;i++){ scanf("%s",str); getchar(); gets(str); int len = strlen(str); str[len]=' '; char ch[5]; int num=0,flag=0; for(int j=0;j<=len;j++){ if(str[j]==' '){ if(flag){ ch[num]='\0'; int v;sscanf(ch,"%d",&v); mp[i].push_back(v); num=0,flag=0; } }else { ch[num++]=str[j]; flag=1; } } } int l = 1,r=n,ans; while(l<=r){ mid = (l+r)>>1; if(sove()){ ans = mid; r=mid-1; }else l=mid+1; } printf("%d\n",ans); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator