SCU - 4439 Vertex Cover (图的最小点覆盖集)

Vertex Cover

frog has a graph with \(n\) vertices \(v(1), v(2), \dots, v(n)\) and \(m\) edges \((v(a_1), v(b_1)), (v(a_2), v(b_2)), \dots, (v(a_m), v(b_m))\).

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 \(2\) integers \(n, m\) (\(2 \leq n \leq 500, 1 \leq m \leq \frac{n(n - 1)}{2}\)). Each of the following \(m\) lines contains \(2\) integers \(a_i, b_i\) (\(1 \leq a_i, b_i \leq n, a_i \neq b_i, \min\{a_i, b_i\} \leq 30\))

Output

For each test, write \(1\) 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
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N = 1e5+5;
const double eps = 1e-8;
int T,n,cnt,m;
bool ok=true;
int k,mi[N];
int a[N],vis[N],match[N];
char str[N],s[N];
vector<int>edg[N];
bool dfs(int now){
    for(int i=0;i<edg[now].size();++i){
        int x=edg[now][i];
        if(!vis[x]){
            vis[x]=1;
            if(!match[x]||dfs(match[x])){
                match[now]=x;
                match[x]=now;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int u,v;
    while(~scanf("%d%d",&n,&m)){
        for(int i=0;i<N;i++)edg[i].clear(),match[i]=0;
        int ans=0;
        while(m--){
            scanf("%d%d",&u,&v);
            edg[u].pb(v);
            edg[v].pb(u);
        }
        for(int i=1;i<=n;i++){
            if(!match[i]){
                met(vis,0);
                if(dfs(i))ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/jianrenfang/p/6791415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值