[匈牙利算法-最小覆盖点]POJ3041题解

[匈牙利算法-最小覆盖点]POJ3041题解

最小点覆盖

例题: POJ3041

理解: 有图G{U,V},求删去最小点可删去所有边的步骤即求最小点覆盖的过程


二分图最大匹配的König定理及其证明(过于困难)->见[http://www.matrix67.com/blog/archives/116]

结论版 König定理 :
  1. 由X侧匹配失败的一点回溯所有边(可见图) 并给走过的点打上标记
  2. X侧未打标记的点和X对侧打标记的点集就是最小点覆盖

post1-img1

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=500+10;

struct Max_Match
{
int n;
bool g[maxn][maxn];
bool vis[maxn];
int left[maxn];

void init(int n)
{
this->n=n;
memset(g,0,sizeof(g));
memset(left,-1,sizeof(left));
}

bool match(int u)
{
for(int v=1;v<=n;v++)if(g[u][v] && !vis[v])
{
vis[v]=true;
if(left[v]==-1 || match(left[v]))
{
left[v]=u;
return true;
}
}
return false;
}

int solve()
{
int ans=0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(match(i)) ans++;
}
return ans;
}
}MM;

int main()
{
int n,k;
scanf("%d%d",&n,&k);
MM.init(n);
while(k--)
{
int u,v;
scanf("%d%d",&u,&v);
MM.g[u][v]=true;
}
printf("%d\n",MM.solve());
return 0;
}

参考博客

http://xchkoo.github.io/post1/ 本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。