Conference

Time limit: 0.5 second
Memory limit: 64 MB
On the upcoming conference were sent M representatives of country A and N representatives of country B (M and N ≤ 1000). The representatives were identified with 1, 2, …, M for country A and 1, 2, …, N for country B. Before the conference K pairs of representatives were chosen. Every such pair consists of one member of delegation A and one of delegation B. If there exists a pair in which both member #i of A and member #j of B are included then #i and #j
can negotiate. Everyone attending the conference was included in at
least one pair. The CEO of the congress center wants to build direct
telephone connections between the rooms of the delegates, so that
everyone is connected with at least one representative of the other
side, and every connection is made between people that can negotiate.
The CEO also wants to minimize the amount of telephone connections.
Write a program which given M, N, K and K pairs of representatives, finds the minimum number of needed connections.

Input

The first line of the input contains M, N and K. The following K lines contain the choosen pairs in the form of two integers p1 and p2, p1 is member of A and p2 is member of B.

Output

The output should contain the minimum number of needed telephone connections.

Sample

input output
3 2 4
1 1
2 1
3 1
3 2
3
Problem Source: Bulgarian National Olympiad Day #1
【分析】最小路径覆盖数 == 总顶点数 - 二分匹配数。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int n,m,k,x,y,pre[N];
//二分图中X集和Y集的节点数各为n、m,边数为k;匹配边集为pre,其中节点i所在的匹配边为(pre[i],i)
bool v[N],a[N][N];
//设二分图相邻矩阵为a,Y集合中节点的访问标志为v,若Y集合中的节点j已访问,则v[j]=true
bool dfs(int i) { //判断以X集合中的节点i为起点的增广路径是否存在
int j;
for(j=; j<=m; j++) {
if(!v[j]&&a[i][j]) { //搜索所有与i相邻的未访问点
v[j]=;//访问节点j
if(pre[j]==-||dfs(pre[j])) {
//若j的前驱是未盖点或者存在由j的前驱出发的增广路径,则设定(i,j)为匹配边,返回成功标志
pre[j]=i;
return true;
}
}
}
return false;//返回失败标志
} int main() {
int i,ans;
scanf("%d%d%d",&n,&m,&k);
memset(a,,sizeof(a));//二分图的相邻矩阵初始化
memset(pre,-,sizeof(pre));//匹配边集初始化为空
for(i=; i<=k; i++) {
scanf("%d%d",&x,&y);
a[x][y]=;
}
ans=;//匹配边数初始化为0
for(i=; i<=n; i++) { //枚举X集的每个节点
memset(v,,sizeof(v));//设Y集合中的所有节点的未访问标志
if(dfs(i)) ans++;//若节点i被匹配边覆盖,则匹配边数+1
}
printf("%d\n",n+m-ans);
return ;
}

timus 1109 Conference(二分图匹配)的更多相关文章

  1. 1109. Conference(二分图)

    1109 二分图的模板题 不过这题题意 我纠结了好久 不知道是不是我对二分图不熟悉的原因 这题就是说 有n+m个人参加会议 要在这n+m中进行通话 求最少的连接数 就是每个人都得被连接上 这样求最大匹 ...

  2. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  3. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  4. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  5. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  6. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  7. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  8. BZOJ 1059 & 二分图匹配

    题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...

  9. 【POJ 3020】Antenna Placement(二分图匹配)

    相当于用1*2的板覆盖给定的h*w的格子里的点,求最少的板.可以把格子相邻的分成两个集合,如下图,0为一个集合,1的为一个,也就是(行数+列数)为奇数的是一个集合,为偶数的为另一个集合.1010101 ...

随机推荐

  1. 恢复drop数据

    select * from recyclebin r where r.original_name = 'MSM_EXAINVITEBIDSCHEMEHEAD' ; flashback table MS ...

  2. tic/toc/cputime测试时间

    cputime测试代码运行时间可能不及tic/toc准确是众所周知的事情.本文并非旧话重提,而是期望起到抛砖引玉的效果,从而找到cputime与tic/toc内在的区别.望不吝赐教! 用tic/toc ...

  3. 聚簇(Cluster)和聚簇表(Cluster Table)

    聚簇(Cluster)和聚簇表(Cluster Table) 时间:2010-03-13 23:12来源:OralanDBA.CN 作者:AlanSawyer 点击:157次 1.创建聚簇 icmad ...

  4. Wythoff's game

    这个问题就是OJ题里出现的取石子游戏,http://en.wikipedia.org/wiki/Wythoff%27s_game. 维基里面的通项公式并不适用于算法求解.需要理解下面两条规律: 1.A ...

  5. jsp弹出Please check the location and try again!对话框

    关闭它的jsp图形模式.myeclipse10中打开jsp文件时,右键open with 选MyEclipse JSP Editor,不选MyEclipse Visual JSP Editor模式.

  6. linux上安装hadoop

    机器准备 物理机器 总 共4台,想配置基于物理机的hadoop集群中包括 4 个 节点: 1 个 Master , 3 个 Salve , 节点之间局域网连接,可以相互 ping 通Ip分布 为192 ...

  7. 使用httputils上传图片到服务器

    //创建httpUtils对象 HttpUtils mRegHttpUtils = new HttpUtils(); //图片路径 String path = "/sdcard/Downlo ...

  8. CODEVS1073 家族 (并查集)

    一道裸的并查集,练练手不错. program CODEVS1073; var i,j,m,n,q,x,y,k1,k2,z:longint; f:..] of longint; function fin ...

  9. 一个比较完整的Inno Setup 安装脚本

    一个比较完整的Inno Setup 安装脚本,增加了对ini文件设置的功能,一个安装包常用的功能都具备了. [Setup] ; 注: AppId的值为单独标识该应用程序. ; 不要为其他安装程序使用相 ...

  10. C# 控件不刷新问题

    /********************************************************************** * C# 控件不刷新问题 * 说明: * 当网络连接出问 ...