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. javascript prototype 剖析

    学过javascript的一定对prototype不陌生,但是这个究竟是个什么东西,就不一定很清楚. 我们先对prototype进行一个定义:每个函数都有一个prototype属性,这个属性是指向一个 ...

  2. RelativeLayout相对布局中拖放控件的办法

    相对布局中拖了一个控件以后,要拖放第二个空间,死活拖不进去.仔细查看了鼠标的状况,发现要把第二个控件拖到第一个控件的周围,才能成功.果然是相对布局.

  3. Netstat命令(一)

    一.查看那些端口号被占用,在命令行中录入:netstat -an ,下图已查找80端口为例 二.查看哪个程序在使用80端口 三.查看占用80端口所对应的PID号 四.打开任务管理器,可以根据PID号找 ...

  4. hdu2795 线段树

    //Accepted 6396 KB 3046 ms //线段树 //由于n只有200000,我们可以知道,当h>200000时,大于200000的部分是没有用的 //所以我们可以用n来创建线段 ...

  5. App跳转至系统Settings

    很多著名和非著名的App有在App内通过某种方式跳转到系统Settings的功能.不论初心和交互,某认为这个功能用的好确实是很方便的,Control Center功能有限,Home键点击起来很累,至于 ...

  6. TPLink 备份文件bin文件解析[续]

    Most routers allow to save and restore configuration from files. This is cool because you can edit t ...

  7. WPF 基础到企业应用系列索引

    转自:http://www.cnblogs.com/zenghongliang/archive/2010/07/09/1774141.html WPF 基础到企业应用系列索引 WPF 基础到企业应用系 ...

  8. 不能使用weak修饰进行声明的类

    These classes include NSTextView, NSFont and NSColorSpace; for the full list, see Transitioning to A ...

  9. python数据分析入门——matplotlib的中文显示问题&最小二乘法

    正在学习<用python做科学计算>,在练习最小二乘法时遇到matplotlib无法显示中文的问题.查资料,感觉动态的加上几条语句是最好,这里贴上全部的代码. # -*- coding: ...

  10. Windows系统下安装Beautiful Soup4的步骤和方法

    1.到http://www.crummy.com/software/BeautifulSoup/网站上下载,最新版本是4.3.2. 2.下载完成之后需要解压缩,假设放到D:\Python27下. 3. ...