Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

HopcorftKarp党飘过

Posted by gnaggnoyil at 2009-11-02 12:54:22 on Problem 2771
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define maxn 510
#define max(a,b) (((a)>(b))?(a):(b))
int adj[maxn+1][maxn+1];
int dx[maxn+1],dy[maxn+1],mx[maxn+1],my[maxn+1],h[maxn+1],p[maxn+1],q[maxn+maxn+1];
char sex[maxn+1],muc[maxn+1][255],spt[maxn+1][255];
int ff=0,rr=0,n=0,testcase=0,ans=0,xn,yn;
int addedge(int u,int v){
  return adj[u][++adj[u][0]]=v,0;
}
bool check(int i,int j){
  return (abs(h[i]-h[j])<=40)&&(sex[i]!=sex[j])&&(strcmp(muc[i],muc[j])==0)&&(strcmp(spt[i],spt[j])!=0);
}
bool bfs(){
  bool ans=false;
  memset(q,0,sizeof(q));
  ff=0,rr=0;
  for(int i=1;i<=xn;i++)
    if(mx[i]+1==0)
      q[++ff]=i;
  for(int i=1;i<=max(xn,yn);i++)
    dx[i]=dy[i]=0;
  for(;rr<ff;){
    int u=q[++rr];
    for(int i=1;i<=adj[u][0];i++){
      int v=adj[u][i];
      if(dy[v]==0){
        dy[v]=dx[u]+1;
        if(my[v]+1==0)
          ans=true;
        else
          dx[my[v]]=dy[v]+1,q[++ff]=my[v];
      }
    }
  }
  return ans;
}
bool dfs(int x){
  for(int i=1;i<=adj[x][0];i++){
    int y=adj[x][i];
    if(dy[y]==dx[x]+1){
      dy[y]=0;
      if((my[y]+1==0)||dfs(my[y])){
        mx[x]=y,my[y]=x;
        return true;
      }
    }
  }
  return false;
}
int main(){
  freopen("in.txt","r",stdin);
  freopen("out.txt","w",stdout);
  scanf("%d",&testcase);
  for(;testcase>0;testcase--){
    memset(adj,0,sizeof(adj));
    memset(dx,0,sizeof(dx));
    memset(dy,0,sizeof(dy));
    memset(mx,0,sizeof(mx));
    memset(my,0,sizeof(my));
    memset(h,0,sizeof(h));
    memset(p,0,sizeof(p));
    memset(q,0,sizeof(q));
    scanf("%d",&n);
    xn=yn=0;
    for(int i=1;i<=n;i++){
      scanf("%d %c %s %s",&h[i],&sex[i],muc[i],spt[i]);
      if(sex[i]=='M')
        p[i]=++xn;
      else
        p[i]=++yn;
      for(int j=1;j<i;j++)
        if(check(i,j))
          if(sex[i]=='M')
            addedge(p[i],p[j]);
          else
            addedge(p[j],p[i]);
    }
    ans=0;
    for(int i=1;i<=max(xn,yn);i++)
      mx[i]=-1,my[i]=-1;
    for(;bfs();)
      for(int i=1;i<=xn;i++)
        if((mx[i]+1==0)&&(dfs(i)))
          ++ans;
    printf("%d\n",n-ans);
  }
  fclose(stdin);
  fclose(stdout);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator