标签:
Description
Input
Output
Sample Input
Sample Output
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 110;
struct Edge{
int from, to, dist, next;
Edge(){}
Edge(int _from,int _to,int _next):from(_from),to(_to),next(_next){}
}edges[maxn*maxn*3]; //direction
struct Point{
int x,y;
}points[maxn*maxn];
int tot , head[maxn];
int linker[3*maxn], used[3*maxn], c[maxn];
void init(){
tot = 0;
memset(head,-1,sizeof(head));
}
void AddEdge(int _u,int _v){
edges[tot] = Edge(_u,_v,head[_u]);
head[_u] = tot++;
}
bool dfs(int u,int _n,int _del){
for(int e = head[u]; e != -1; e = edges[e].next){
int v = edges[e].to;
if(u == points[_del].x && v == points[_del].y) continue;
if(!used[v]){
used[v] = u;
if(linker[v] == -1 || dfs(linker[v],_n,_del)){
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary(int p, int n,int del){
int ret = 0;
memset(linker,-1,sizeof(linker));
for(int i = 1; i <= p; i++){
memset(used,0,sizeof(used));
if(dfs(i,n,del))
ret++;
}
return ret;
}
int main(){
int n, m, T, p, k, cas = 0;
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
int a,b;
init();
for(int i = 1; i <= k; i++){
scanf("%d%d",&a,&b);
AddEdge(a,b);
}
int ans = hungary(n,m,0);
int cc = 1;
for(int i = 1; i <= m; i++){
if(linker[i] != -1){
points[cc].x = linker[i];
points[cc].y = i;
cc++;
}
}
int tmp, num = 0;
for(int i = 1; i < cc; i++){
tmp = hungary(n,m,i);
if(tmp != ans){
num++;
}
}
printf("Board %d have %d important blanks for %d chessmen.\n",++cas,num,ans);
}
return 0;
}
HDU 1281——棋盘游戏——————【最大匹配、枚举删点、邻接表方式】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/4955263.html