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
题意:
最小边覆盖
tip:
total点数-二分图最佳匹配。
匈牙利算法。。。
//最小边覆盖:total点数-二分图最大匹配。最大匹配:匈牙利算法
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 2e6+10;
int n,m,k,tot,head[maxn];
struct node{
int to,next;
}edges[maxn];
void add(int u,int v){
edges[tot].to = v;edges[tot].next = head[u];head[u] = tot++;
}
void init(){
memset(head,-1,sizeof(head));
tot = 0;
for(int i = 1; i<= k ; i++){
int l,r;
scanf("%d%d",&l,&r);
add(l,r+m);
}
}
bool use[maxn];
int linker[maxn];
//linker是右边序列得左匹配项,匈牙利算法要求的是每次增增广看是否可以匹配边和非匹配边依次,最后实现比上一次多一个点倍包含,
//所以用linker就是,上次匹配完成得,这次直接走,相当于走匹配, 而左边点得匹配,每次循环都置零,
//所以相当于非匹配边,只要有一个左边点匹配到了新的右侧点,就相当于增广成功
bool dfs(int u){
int v;
for(int k = head[u]; k != -1; k = edges[k].next){
int to = edges[k].to;
if(!use[to]){
use[to] = true;
if(linker[to] == -1 || dfs(linker[to])){
linker[to] = u;
return true;
}
}
}
return false;
}
void sov(){
int res = 0;
memset(linker,-1,sizeof(linker));
for(int i = 1 ; i <= m ; i++){
memset(use,0,sizeof(use));
if(dfs(i)){
res++;
}
}
printf("%d\n",n+m-res);
}
int main(){
while(~scanf("%d%d%d",&m,&n,&k)){
init();
sov();
}
}