SCU 4439 vertex cover (minimum coverage of a bipartite graph)

Source: Internet
Author: User

Question: at least one endpoint of each side needs to be colored. If you want to paint at least a few points

Idea: minimum vertex coverage: associate each edge with at least one of the vertices with the least vertex. This is obviously the minimum vertex coverage problem of the bare path;

Reference: Bipartite Graph

Code:

#include<iostream>#include<algorithm>#include<cstdio>#include<stdio.h>#include<string.h>#include<queue>#include<cmath>#include<map>#include<set>#include<vector>using namespace std;typedef unsigned long long ull;const int maxn = 500 + 10;const ull seed = 131;struct Edge{    int v, next;}edge[maxn * maxn * 2];int head[maxn], match[maxn], vis[maxn], tot;void addEdge(int u, int v){    edge[tot].v = v;    edge[tot].next = head[u];    head[u] = tot++;}bool dfs(int u){    for(int i = head[u]; i != -1; i = edge[i].next){        int v = edge[i].v;        if(!vis[v]){            vis[v] = 1;            if(match[v] == -1 || dfs(match[v])){                match[v] = u;                match[u] = v;                return true;            }        }    }    return false;}int solve(int n){    int ans = 0;    memset(match, -1, sizeof(match));    for(int i = 1; i <= n; i++){        if(match[i] == -1){            memset(vis, 0, sizeof(vis));            if(dfs(i)) ans++;        }    }    return ans;}int main(){    int n, m;    while(~scanf("%d%d", &n, &m)){        memset(head, -1, sizeof(head));        tot = 0;        for(int i = 1; i <= m; i++){            int u, v;            scanf("%d%d", &u, &v);            addEdge(u, v);            addEdge(v, u);        }        printf("%d\n", solve(n));    }    return 0;}

 

SCU 4439 vertex cover (minimum coverage of a bipartite graph)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.