SCU - 4439 Vertex Cover (二分图最小点覆盖 匈牙利算法)

本文探讨了图论中的最小点覆盖问题,即如何在图中找到最少数量的顶点,使得每条边至少包含一个这些顶点。通过使用匈牙利算法解决最大二分图匹配问题,我们可以有效地求解最小点覆盖。文章提供了详细的算法实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vertex Cover

frog has a graph with nn vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and mm edges (v(a1),v(b1)),(v(a2),v(b2)),…,(v(am),v(bm))(v(a1),v(b1)),(v(a2),v(b2)),…,(v(am),v(bm)).

She would like to color some vertices so that each edge has at least one colored vertex.

Find the minimum number of colored vertices.

Input

The input consists of multiple tests. For each test:

The first line contains 22 integers n,mn,m (2≤n≤500,1≤m≤n(n−1)22≤n≤500,1≤m≤n(n−1)2). Each of the following mm lines contains 22 integers ai,biai,bi (1≤ai,bi≤n,ai≠bi,min{ai,bi}≤301≤ai,bi≤n,ai≠bi,min{ai,bi}≤30)

Output

For each test, write 11 integer which denotes the minimum number of colored vertices.

Sample Input

    3 2
    1 2
    1 3
    6 5
    1 2
    1 3
    1 4
    2 5
    2 6

Sample Output

    1
    2

题目大意:有n个点,m条边,每条边最少有一个点上色,问最少需要个多少个点上色

思路:最小点覆盖 ==最大二分图匹配  匈牙利算法模板

代码:

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
const int N=10005;
vector<int>e[N];
int vis[N],ma[N];
int find(int x)
{
    for(int i=0;i<e[x].size();i++)
    {
        int v=e[x][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(ma[v]==0||find(ma[v]))
            {
                ma[v]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(ma,0,sizeof(ma));
        for(int i=0;i<=n;i++) e[i].clear();
        int x,y;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            e[x].push_back(y);
            e[y].push_back(x);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i)) ans++;
        }
        printf("%d\n",ans/2);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值