标签:table contains which line nbsp air term park 匹配
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3445 Accepted Submission(s): 1452
Special Judge
//简单的二分匹配,相邻的且可用的网格之间建边,求玩=完二分图之后link数组存储的就是答案。 #include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; int vis[10004],link[10004],nu1[10004],nu2[10004],pond[10004]; int Mu,Mv,n,m,k,last; vector<int>g[10004]; bool dfs(int x) { for(int i=0;i<g[x].size();i++){ int y=g[x][i]; if(!vis[y]){ vis[y]=1; if(link[y]==-1||dfs(link[y])){ link[y]=x; return 1; } } } return 0; } int Maxcon() { int ans=0; memset(link,-1,sizeof(link)); for(int i=1;i<=n*m;i++){ if(pond[i]) continue; memset(vis,0,sizeof(vis)); if(dfs(i)) ans++; } return ans; } int main() { while(scanf("%d%d",&n,&m)&&(n+m)){ int x,y; memset(pond,0,sizeof(pond)); scanf("%d",&k); while(k--){ scanf("%d%d",&x,&y); pond[(x-1)*m+y]=1; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ int id=(i-1)*m+j; g[id].clear(); if(i>1&&pond[id-m]==0) g[id].push_back(id-m); if(j>1&&pond[id-1]==0) g[id].push_back(id-1); if(i<n&&pond[id+m]==0) g[id].push_back(id+m); if(j<m&&pond[id+1]==0) g[id].push_back(id+1); } } int ans=Maxcon()/2; printf("%d\n",ans); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ int id=(i-1)*m+j; if(link[id]==-1||link[link[id]]==-1) continue; printf("(%d,%d)--(%d,%d)\n",i,j,(link[id]-1)/m+1,link[id]%m==0?m:link[id]%m); link[id]=link[link[id]]=-1; } } printf("\n"); } return 0; }
标签:table contains which line nbsp air term park 匹配
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6504690.html