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;
}
 
时间: 2024-10-11 06:23:19

SCU - 4439 Vertex Cover (图的最小点覆盖集)的相关文章

四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)

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

scu oj 4439 : Vertex Cover(2015年四川省程序ACM设计竞赛D题 )

一般图的最小点覆盖问题是是一个npc问题,目前哈没有比较好的多项式的算法.但是这题有一点特殊的地方,每条边必定包含前面30个点的的一个,所以这题可以枚举钱30个点的选和不选的状态,后面的点对应的状态就唯一了.    所以这题就是  dfs+可行性减枝和答案最优减枝. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #i

Jewelry Exhibition(最小点覆盖集)

Jewelry Exhibition 时间限制: 1 Sec  内存限制: 64 MB提交: 3  解决: 3[提交][状态][讨论版] 题目描述 To guard the art jewelry exhibition at night, the security agency has decided to use a new laser beam system, consisting of sender-receiver pairs. Each pair generates a strip o

POJ2226 Muddy Fields(二分图最小点覆盖集)

题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作为XY部,每一块泥地看作边.这样就构造出了一个二分图. 那么,问题就是在这个二分图中就是选出最少的点覆盖所有的边,即二分图最小点覆盖集,而二分图最小点覆盖集=二分图最大匹配. 1 #include<cstdio> 2 #include<cstring> 3 #include<qu

hdu 1151 或 poj 1422 二分图 最小点覆盖集

最小点覆盖集的裸题,只要“拆点建边”然后求出最大匹配,则:最小点覆盖集的大小 = 点数 - 最大匹配 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 121; 7 const int M = 5000; 8 bool visit[N]; 9 int mark[N]; 10 int head[N]; 11 int

POJ 1463 Strategic game 最小点覆盖集(树形dp)

点击打开链接 Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6105   Accepted: 2808 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is v

POJ 3401 Asteroids 求最小点覆盖集

把行和列都看做是点,小行星看成是边的话,那么这个显然就是求一个最小点覆盖集的问题. 最小点覆盖 == 最大匹配 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #inc

ACM/ICPC 之 机器调度-匈牙利算法解最小点覆盖集(DFS)(POJ1325)

//匈牙利算法-DFS //求最小点覆盖集 == 求最大匹配 //Time:0Ms Memory:208K #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; #define MAX 105 #define INF 0x3f3f3f3f int n,m,k; int gp[MAX][MAX]; bool sx[MAX],s

Strategic game POJ - 1463 【最小点覆盖集】

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to