这道题题是很经典的建模的题目,也是比较基础的二分匹配题目。
二分最小覆盖就是说选择尽可能少的点,然后使得每条边至少有一个端点被选中。可以证明的是最小覆盖数就是最大匹配数。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510;
int n, k;
int g[N][N], cy[N];
bool used[N];
bool dfs( int u )
{
for ( int v = 1; v <= n; ++v ) if ( g[u][v] && !used[v] ) {
used[v] = 1;
if ( cy[v] == -1 || dfs( cy[v] ) ) {
cy[v] = u;
return 1;
}
}
return false;
}
int match()
{
int res = 0;
memset(cy, -1, sizeof(cy));
for ( int i = 1; i <= n; ++i ) {
memset( used, 0, sizeof(used));
if ( dfs(i) ) res++;
}
return res;
}
int main()
{
while ( scanf("%d%d",&n, &k) == 2 ) {
memset(g, 0, sizeof(g));
while ( k-- ) {
int u, v;
scanf("%d%d", &u, &v);
g[u][v] = 1;
}
printf("%d\n", match());
}
}
本文介绍了一种经典的二分匹配算法实现,并通过一个具体的例子解释了如何找到一个图中的最小覆盖集合。最小覆盖问题的目标是在图中选择尽可能少的节点,确保所有边至少有一个端点被选中。
2368

被折叠的 条评论
为什么被折叠?



