Hihocoder1127 bipartite graph 3. bipartite graph minimum vertex coverage and Maximum Independent Set

Source: Internet
Author: User

Ideas:

For images without isolated points, | maximum matching | + | minimum edge coverage | = | v |, | maximum independent set | + | minimum vertex overwrite |=| v |. For a bipartite graph, | maximum match |=| minimum vertex overwrite |. (V is the vertex set of the graph)

Implementation:

 1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 using namespace std; 5  6 const int MAXN = 1005; 7 vector<int> G[MAXN]; 8 int n, m; 9 bool used[MAXN];10 int match[MAXN];11 12 bool dfs(int v)13 {14     used[v] = true;15     for (int i = 0; i < G[v].size(); i++)16     {17         int u = G[v][i];18         int w = match[u];19         if (w == -1 || !used[w] && dfs(w))20         {21             match[v] = u;22             match[u] = v;23             return true;24         }25     }26     return false;27 }28 29 int max_match()30 {31     int res = 0;32     for (int i = 1; i <= n; i++)33     {34         if (match[i] == -1)35         {36             memset(used, 0, sizeof(used));37             if (dfs(i))38                 res++;39         }40     }41     return res;42 }43 44 int main()45 {46     cin >> n >> m;47     int a, b;48     for (int i = 0; i < m; i++)49     {50         cin >> a >> b;51         G[a].push_back(b);52         G[b].push_back(a);53     }54     memset(match, -1, sizeof(match));55     int ans = max_match();56     cout << ans << endl;57     cout << n - ans << endl;58     return 0;59 }

 

Hihocoder1127 bipartite graph 3. bipartite graph minimum vertex coverage and Maximum Independent Set

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.