Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 656 Accepted Submission(s): 329
#include <cstdio> #include <cstdlib> #include <cstring> #include <cstring> const int maxn=1111; int g[maxn][maxn]; int cx[maxn],cy[maxn],vst[maxn]; int nx,ny; int findpath(int u){ for(int v=0;v<ny;v++){ if(!vst[v]&&g[u][v]){ vst[v]=1; if(cy[v]==-1||findpath(cy[v])){ cy[v]=u,cx[u]=v; return 1; } } } return 0; } int MaxMatch(){ int ret=0; memset(cx,-1,sizeof(cx)); memset(cy,-1,sizeof(cy)); for(int i=0;i<nx;i++) if(cx[i]==-1){ memset(vst,0,sizeof(vst)); if(findpath(i)) ret++; } return ret; } struct node{ int x,y; }mx[maxn],my[maxn]; int main(){ //freopen("1009.in","r",stdin); int n,m; while(scanf("%d %d",&n,&m)&&n+m){ for(int i=0,a,b;i<n;i++){ scanf("%d %d",&a,&b); mx[i].x=a,mx[i].y=b; } for(int i=0,a,b;i<m;i++){ scanf("%d %d",&a,&b); my[i].x=a,my[i].y=b; } memset(g,0,sizeof(g)); for(int i=0,x1,y1;i<n;i++){ x1=mx[i].x,y1=mx[i].y; for(int j=0,x2,y2;j<m;j++){ x2=my[j].x,y2=my[j].y; if(x1==x2&&y1==y2 ||x1==x2&&y1==y2+1 ||x1+1==x2&&y1==y2 ||x1+1==x2&&y1==y2+1) g[i][j]=1; } } nx=n,ny=m; printf("%d\n",n+m-MaxMatch()); } return 0; }
#include <cstdlib> #include <cstdio> #include <vector> #include <cstring> #include <iostream> #define clr(x,k) memset((x),(k),sizeof(x)) #define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it) using namespace std; typedef vector<int> vi; const int maxn=1000; vector<int> gx[maxn]; int cx[maxn],cy[maxn],vst[maxn]; int nx,ny; int findpath_Vector(int u){ foreach(it,gx[u]){ if(!vst[*it]){ vst[*it]=1; if(cy[*it]==-1||findpath_Vector(cy[*it])){ cx[u]=*it; cy[*it]=u; return 1; } } } return 0; } int maxMatch_Vector(){ int ret=0; clr(cx,-1),clr(cy,-1); for(int i=0;i<nx;i++) if(cx[i]==-1){ clr(vst,0); if(findpath_Vector(i)) ret++; } return ret; } struct node{ int x,y; }mx[maxn],my[maxn]; int main(){ // freopen("1009.in","r",stdin); int n,m; while(scanf("%d %d",&n,&m)&&n+m){ for(int i=0,a,b;i<n;i++){ scanf("%d %d",&a,&b); mx[i].x=a,mx[i].y=b; } for(int i=0,a,b;i<m;i++){ scanf("%d %d",&a,&b); my[i].x=a,my[i].y=b; } for(int i=0;i<n;i++) gx[i].clear(); for(int i=0,x1,y1;i<n;i++){ x1=mx[i].x,y1=mx[i].y; for(int j=0,x2,y2;j<m;j++){ x2=my[j].x,y2=my[j].y; if(x1==x2&&y1==y2 ||x1==x2&&y1==y2+1 ||x1+1==x2&&y1==y2 ||x1+1==x2&&y1==y2+1) gx[i].push_back(j); } } nx=n,ny=m; printf("%d\n",n+m-maxMatch_Vector()); } return 0; }
#include <cstdlib> #include <cstdio> #include <vector> #include <queue> #include <cstring> #include <iostream> #define clr(x,k) memset((x),(k),sizeof(x)) #define foreach(it,c) for(vi::iterator it = (c).begin();it != (c).end();++it) using namespace std; typedef vector<int> vi; const int INF=1<<28; const int maxn=1000; int cx[maxn],cy[maxn]; vi gx[maxn]; int dx[maxn],dy[maxn]; int dis,nx; int vst[maxn]; bool searchpath(){ queue<int> Q; dis=INF; clr(dx,-1),clr(dy,-1); for(int i=0;i<nx;i++){ if(cx[i]==-1) Q.push(i),dx[i]=0; } while(!Q.empty()){ int u=Q.front();Q.pop(); if(dx[u]>dis) break; foreach(it,gx[u]) if(dy[*it]==-1){ dy[*it]=dx[u]+1; if(cy[*it]==-1) dis=dy[*it]; else{ dx[cy[*it]]=dy[*it]+1; Q.push(cy[*it]); } } } return dis!=INF; } int hop_Findpath_Vector(int u){ foreach(it,gx[u]){ if(!vst[*it]&&dy[*it]==dx[u]+1){//说明这是一条增广路径。但还需判断 vst[*it]=1; if(cy[*it]!=-1&&dy[*it]==dis) //出现这种情况。说明不是增广路, 因为最后一条增路是 cy[v]==-1&&dy[v]==dis continue; if(cy[*it]==-1||hop_Findpath_Vector(cy[*it])){ cy[*it]=u,cx[u]=*it; return 1; } } } return 0; } int hopcroft_MaxMatch_Vector(){ int res=0; clr(cx,-1),clr(cy,-1); while(searchpath()){ clr(vst,0); for(int i=0;i<nx;i++){ if(cx[i]==-1){ res+=hop_Findpath_Vector(i); } } } return res; } struct node{ int x,y; }mx[maxn],my[maxn]; int main(){ //freopen("1009.in","r",stdin); int n,m; while(scanf("%d %d",&n,&m)&&n+m){ for(int i=0,a,b;i<n;i++){ scanf("%d %d",&a,&b); mx[i].x=a,mx[i].y=b; } for(int i=0,a,b;i<m;i++){ scanf("%d %d",&a,&b); my[i].x=a,my[i].y=b; } for(int i=0;i<n;i++) gx[i].clear(); for(int i=0,x1,y1;i<n;i++){ x1=mx[i].x,y1=mx[i].y; for(int j=0,x2,y2;j<m;j++){ x2=my[j].x,y2=my[j].y; if(x1==x2&&y1==y2 ||x1==x2&&y1==y2+1 ||x1+1==x2&&y1==y2 ||x1+1==x2&&y1==y2+1) gx[i].push_back(j); } } nx=n; printf("%d\n",n+m-hopcroft_MaxMatch_Vector()); } return 0; }