分析:将行和列缩点,即行对应二分图的X部,列对应二分图的Y部,然后交点为连接该行和该列的一条边。匹配时每点都会把整行整列占了,因此就不会出现冲突了。

传送门:hdu1281 棋盘游戏

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 110
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
int match[N],vis[N],n,m,k;
int g[N][N],a[N*N][],x,y;
int dfs(int u)
{
for(int i=;i<=m;i++)
{
if(!vis[i]&&g[u][i])
{
vis[i]=;
if(match[i]==-||dfs(match[i]))
{
match[i]=u;
return ;
}
}
}
return ;
}
int hungary()
{
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
if(dfs(i))ans++;
}
return ans;
}
int main()
{
int cas=;
while(scanf("%d%d%d",&n,&m,&k)>)
{
memset(g,,sizeof(g));
for(int i=;i<=k;i++)
{
scanf("%d%d",&x,&y);
g[x][y]=;
a[i][]=x;
a[i][]=y;
}
int res=hungary();
int ans=;
for(int i=;i<=k;i++)
{
g[a[i][]][a[i][]]=;
int maxn=hungary();
if(maxn<res)ans++;
g[a[i][]][a[i][]]=;
}
printf("Board %d have %d important blanks for %d chessmen.\n",cas++,ans,res);
}
}

传送门:hdu2819 Swap

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 110
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
int match[N],vis[N],n,m;
int g[N][N],a[N*N][];
int dfs(int u)
{
for(int i=;i<=n;i++)
{
if(!vis[i]&&g[u][i])
{
vis[i]=;
if(match[i]==-||dfs(match[i]))
{
match[i]=u;
return ;
}
}
}
return ;
}
int hungary()
{
memset(match,-,sizeof(match));
int ans=;
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
if(dfs(i))ans++;
}
return ans;
}
int main()
{
int cas=;
while(scanf("%d",&n)>)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&g[i][j]);
int res=hungary();
if(res!=n)
{
puts("-1");
continue;
}
int sum=,i,j;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if(match[j]==i)break;
}
if(i==j)continue;
sum++;
a[sum][]=i;
a[sum][]=j;
swap(match[i],match[j]);
}
printf("%d\n",sum);
for(int i=;i<=sum;i++)
printf("C %d %d\n",a[i][],a[i][]);
}
}

hdu1281+hdu2819(最大匹配数)的更多相关文章

  1. poj3041-Asteroids , 二分图的最小顶点覆盖数 = 最大匹配数

    点击打开链接 Konig定理:二分图的最小顶点覆盖数 = 二分图的最大匹配数 题意: 在N*N的网络中有K颗小行星.小行星i的位置是(Ri, Ci).如今有一个强力的武器可以用一发光束将一整行或一整列 ...

  2. HDU 1068 Girls and Boys(最大独立集合 = 顶点数 - 最大匹配数)

    HDU 1068 :题目链接 题意:一些男孩和女孩,给出一些人物关系,然后问能找到最多有多少个人都互不认识. 转换一下:就是大家都不认识的人,即最大独立集合 #include <iostream ...

  3. (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...

  4. hdu-2768-Cat vs. Dog(二分图-最大匹配数)

    题意: 有猫C个和狗D个,有V个投票人,每个人喜欢猫讨厌狗或则喜欢狗讨厌猫! 求最多能满足多少投票人. 分析: 两个投票者矛盾的话就连一条边,总数减去最大匹配数/2就是要求的答案 // File Na ...

  5. HDU 4160 Dolls (最小路径覆盖=顶点数-最大匹配数)

    Dolls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  6. UVALive 2523 Machine Schedule(二分图求最大匹配数)

    题意:有两台机器,上面有多个工作区域,有多个任务,分别可以在两台机器的某一个区域上完成,两台机器一开始都在0区域上工作,每次更改区域,都会重新启动一次,让我们求出最小的重启次数. 思路:将两个区域连线 ...

  7. POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)

    题目链接: http://poj.org/problem?id=2771 Description Frank N. Stein is a very conservative high-school t ...

  8. poj3020 Antenna Placement 匈牙利算法求最小覆盖=最大匹配数(自身对应自身情况下要对半) 小圈圈圈点

    /** 题目:poj3020 Antenna Placement 链接:http://poj.org/problem?id=3020 题意: 给一个由'*'或者'o'组成的n*m大小的图,你可以用一个 ...

  9. Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游

    /** 题目:Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游 链接:https://vjudge.net/problem/UVA ...

  10. HDU - 2063 过山车(最大匹配数)(模板)

    1.男生女生一起坐过山车,每一排有两个座位,但是有个条件,就是每个女生必须找个男生做同伴一起(但是女生只愿意和某几个男生中的一个做同伴),求最多可以有多少对男女生组合坐上过山车. 2.二分图的最大匹配 ...

随机推荐

  1. 基于visual Studio2013解决C语言竞赛题之1015日期计算

        题目 解决代码及点评 /* 15. 已知某年不是闰年,给定该年某一天的月份和日期, 求这一天是该年的第几天. */ #include <stdio.h> #incl ...

  2. Struts2运行机制(MVC)的分析:

    C:(controller)控制器          M:(model)模型处理    V:(view)视图 Struts 2 的运行过程:     核心控制器是FilterDispatcher会过滤 ...

  3. POJ - 1006 Biorhythms 周期相遇 两个思路程序

    Description Some people believe that there are three cycles in a person's life that start the day he ...

  4. 华为手机logcat不出日志解决方案

    解决方法:在拨打电话界面,录入*#*#2846579#*#* 自动进入开发界面菜单,进入第一个,选择开启logcat.

  5. C#日志工具汇总

    log4net          log4net是一个可以帮助程序员把日志信息输出到各种不同目标的.net类库.它可以容易的加载到开发项目中,实现程序调试和运行的时候的日志信息输出,提供了比.net自 ...

  6. Inhouse interview(websense)

    1.Tell me about yourself? My name is xxx,i 'm from xxx. now , I am a postgratuation and my major sub ...

  7. haproxy timeout server 46000 后台超时时间

    [root@wx03 ~]# sh ./1.sh Wed Jul 6 19:54:40 CST 2016 <html><body><h1>504 Gateway T ...

  8. [置顶] 64位Win2008_VS2012使用ODP.NET遭遇问题和解决办法

    最近为使用Oracle11G数据库做个快速开发的小程序,使用64位Win2008+Vs2012环境,结果碰壁连环,幸好不算太笨,终于解决了,特记录一下. 测试环境: Oracle11g (11.2.0 ...

  9. Go语言 关于go error处理风格的一些讨论和个人观点(上)

    原创文章.转载请注明出处:server非业余研究-sunface 近期看谷歌go group里面有非常多讨论go error处理风格的问题,颇有启示.如今跟大家分享一下.首先请看一个提问: Hi fo ...

  10. OO的ALV隐藏工具栏的form

    OO的ALV隐藏工具栏: ***展示数据 CALL METHOD gr_alvgrid->set_table_for_first_display EXPORTING is_variant = g ...