HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)
来源:互联网 发布:域名注册人查询 编辑:程序博客网 时间:2024/06/05 09:26
<p style="margin: 10px auto; line-height: 19.5px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(40, 85, 126);"> 要输出任意一组解。</p><p style="margin: 10px auto; line-height: 19.5px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(40, 85, 126);">一开始时两边都是n*m-k个点做的,答案输出一半,但是错掉了,匹配数没有问题,就是输出解会出错。</p><p style="margin: 10px auto; line-height: 19.5px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(40, 85, 126);">后来按照奇偶分成两部分就可以了</p>
#include<stdio.h>#include<algorithm>#include<iostream>#include<string.h>#include<vector>using namespace std;const int MAXN=510;int uN,vN;int g[MAXN][MAXN];int linker[MAXN];bool used[MAXN];bool dfs(int u){ for(int v=0;v<vN;v++){ if(g[u][v]&&!used[v]){ used[v]=true; if(linker[v]==-1||dfs(linker[v])) { linker[v]=u; return true; } } } return false;}int hungary(){ int res=0; memset(linker,-1,sizeof(linker)); for(int u=0;u<uN;u++){ memset(used,false,sizeof(used)); if(dfs(u)) res++; } return res;}int a[110][110];int b[100];int main(){ int n,m,k; int u,v; while(scanf("%d%d",&n,&m)==2){ if(n==0&&m==0) break; scanf("%d",&k); memset(a,0,sizeof(a)); while(k--){ scanf("%d%d",&u,&v); u--;v--; a[u][v]=-1; } int index=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(a[i][j]!=-1) { b[index]=i*m+j; a[i][j]=index++; } } } uN=vN=index; memset(g,0,sizeof(g)); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(a[i][j]!=-1&&(i+j)%2==1) if(a[i][j]!=-1 && (i+j)%2==1) { u = a[i][j]; if(i > 0 && a[i-1][j]!=-1) g[u][a[i-1][j]]=1; if(i < n-1 && a[i+1][j]!=-1) g[u][a[i+1][j]]=1; if(j > 0 && a[i][j-1]!=-1) g[u][a[i][j-1]]=1; if(j < m-1 && a[i][j+1]!=-1) g[u][a[i][j+1]]=1; } int ans = hungary(); printf("%d\n",ans); for(int i = 0;i <vN;i++) if(linker[i]!=-1) { int x1 = b[i]/m; int y1 = b[i]%m; int x2 = b[linker[i]]/m; int y2 = b[linker[i]]%m; printf("(%d,%d)--(%d,%d)\n",x1+1,y1+1,x2+1,y2+1); } printf("\n"); } return 0;}
0 0
- HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)
- HDU 1507 Uncle Tom's Inherited Land*(二分图最大匹配:输出一组解)
- HDU 1507 Uncle Tom's Inherited Land*(二分图最大匹配:输出一组解)
- hdu 1507 Uncle Tom's Inherited Land* (二分匹配)
- hdu 1507 Uncle Tom's Inherited Land* (二分匹配)
- HDU--1507 Uncle Tom's Inherited Land* (二分匹配)
- HDU-1507 Uncle Tom's Inherited Land* 二分图匹配
- Hdu 1507 Uncle Tom's Inherited Land*【二分匹配】
- hdu 1507 Uncle Tom's Inherited Land*(二分图)
- 杭电 hdu 1507 Uncle Tom's Inherited Land*(二分图,最大匹配)
- HDU 1507 Uncle Tom's Inherited Land* 二分图最大匹配(基础题)
- hdu 1507 Uncle Tom's Inherited Land*(二分图最大匹配,黑白染色)
- Uncle Tom's Inherited Land*(二分匹配)
- hdu 1507 Uncle Tom's Inherited Land* 最大匹配
- hdu 1507 Uncle Tom's Inherited Land(行列匹配)
- HDU 1507 Uncle Tom's Inherited Land*
- hdu 1507 Uncle Tom's Inherited Land*
- Hdu 1507 Uncle Tom's Inherited Land*
- 修改tomcat默认的编码方式
- 将图片等分成若干张分图
- Eclipse下用javadoc导出Android项目的帮助文档(一)
- json格式及json在Python和C/C++中的使用
- 为什么要用UML建模之建模的重要性
- HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)
- Flume NG源码分析(六)应用程序使用的RpcClient设计
- 【归纳总结】Unix/linux下的进程管理(二):创建进程的函数及其应用、对比
- 译密码
- 分支-06 成绩转换(15)
- 自定义新浪微博分享按钮样式
- 关于“The type **** is not accessible due to restriction on required library”问题的解决
- Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机
- 数据结构与算法-----堆栈-使用数组(顺序结构)实现