题意:每个女人有感兴趣的k个男人,过山车两人一组,求最大匹配数。

解题关键:二分图最大匹配。匈牙利算法求解。

1、链式前向星建图

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#define maxn 250020
using namespace std;
typedef long long ll;
int n,m,k,a,b;
struct Edge{
int nxt;
int to;
int w;
}e[maxn];
int head[maxn],cnt;
void add_edge(int u,int v){
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
int pre[maxn];
bool vis[maxn];
bool dfs(int u){
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].to;
if(!vis[v]){
vis[v]=true;
if(pre[v]==-||dfs(pre[v])){
pre[v]=u;
//pre[u]=v;
return true;
}
}
}
return false;
} int hungary(){
int ans=;
memset(pre,-,sizeof pre);
for(int i=;i<=m;i++) {
if(pre[i]==-) {
memset(vis,,sizeof vis);
if(dfs(i)) ans++;
}
}
return ans;
}
int main(){
while(scanf("%d",&k)!=EOF&&k){
memset(head, -, sizeof head);
cnt=;
scanf("%d%d",&m,&n);
for(int i=;i<k;i++){
scanf("%d%d",&a,&b);
add_edge(a,b+m);
}
int ans=hungary();
printf("%d\n",ans);
}
return ;
}

2、邻接矩阵建图

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#define maxn 505
using namespace std;
typedef long long ll;
bool G[maxn][maxn];
int pre[maxn];
bool vis[maxn];
int n,m,k;
bool dfs(int u){
for(int i=;i<=n;i++){
if(G[u][i]&&!vis[i]){
vis[i]=true;
if(pre[i]==-||dfs(pre[i])){
pre[i]=u;
return true;
}
}
}
return false;
} int hungary(){
int num=;
memset(pre,-,sizeof pre);
for(int i=;i<=m;i++){
memset(vis,,sizeof vis);
if(dfs(i)) num++;
}
return num;
}
int main(){
int u,v;
while(scanf("%d",&k)!=EOF&&k){
scanf("%d%d",&m,&n);
memset(G,,sizeof G);
for(int i=;i<k;i++){
scanf("%d%d",&u,&v);
G[u][v]=;
}
int ans=hungary();
printf("%d\n",ans);
}
return ;
}

[hdu2063]过山车(二分图匹配)的更多相关文章

  1. HDU2063 过山车(二分匹配)

    过山车 HDU - 2063 RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做part ...

  2. hdu-2063 过山车(二分图)

    Time limit1000 ms Memory limit32768 kB RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不 ...

  3. HDU2063 过山车

    过山车 RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是, ...

  4. hdu 2063 过山车 二分匹配(匈牙利算法)

    简单题hdu2063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 过山车 Time Limit: 1000/1000 MS (Java/Ot ...

  5. HDU- 2063 过山车

    http://acm.hdu.edu.cn/showproblem.php?pid=2063 过山车//我的第一个二分匹配 Time Limit: 1000/1000 MS (Java/Others) ...

  6. HDU 2063 过山车 二分匹配

    解题报告:有m个女生和n个男生要结成伴坐过山车,每个女生都有几个自己想选择的男生,然后要你确定最多能组成多少对组合. 最裸的一个二分匹配,这是我第一次写二分匹配,给我最大的感受就是看那些人讲的匈牙利算 ...

  7. hdu 2063 过山车 (二分图,最大匹配)

    过山车Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  8. luogu3386 【模板】二分图匹配 匈牙利算法 hdu2063 过山车 dinic

    luogu 匈牙利算法 #include <iostream> #include <cstring> #include <cstdio> using namespa ...

  9. HDU 2063 过山车(二分匹配入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 二分匹配最大匹配数简单题,匈牙利算法.学习二分匹配传送门:http://blog.csdn.ne ...

随机推荐

  1. TP框架---thinkphp模型

    1.获取系统常量信息的方法:在控制器DengLuController里面下写入下面的方法,然后调用该方法. public function test() { //echo "这是测试的&qu ...

  2. ch.poweredge.ntlmv2-auth

    <dependency> <groupId>ch.poweredge.ntlmv2-auth</groupId> <artifactId>ntlmv2- ...

  3. kafka source type

    https://flume.apache.org/FlumeUserGuide.html # example.conf: A single-node Flume configuration # Nam ...

  4. Javascript模块化编程-require.js[3]

    很多情况下,JS都是放到一个或者多个文件里,只要加载这些文件就可以了. 但是对于一些小型项目而言,这种写法是没有任何问题的. 但是对于某些大型网站,JS的量是很大的,如果还采用这种方式,网站时常在加载 ...

  5. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  6. 51Nod 1486 大大走格子 —— 组合数学

    题目链接:https://vjudge.net/problem/51Nod-1486 1486 大大走格子 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: ...

  7. smokeping再次部署遇到的问题记录

    问题1: Can't locate Sys/Syslog.pm in @INC (@INC contains: /opt/smokeping_workspace/thirdparty/lib/perl ...

  8. BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线:spfa + 二分【路径中最大边长最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1614 题意: 给你一个无向图,n个点,m条边. 你需要找出一条从1到n的路径,使得这条路径 ...

  9. canvas刮刮卡

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  10. linux cpu占用100%排查

    某服务器上部署了若干tomcat实例,即若干垂直切分的Java站点服务,以及若干Java微服务,突然收到运维的CPU异常告警. 问:如何定位是哪个服务进程导致CPU过载,哪个线程导致CPU过载,哪段代 ...