标签:独立 out set code pre bool 最小边覆盖 int 最大
思路:
对于不存在孤立点的图,|最大匹配| + |最小边覆盖| = |V|,|最大独立集| + |最小顶点覆盖| = |V|。对于二分图而言,|最大匹配| = |最小顶点覆盖|。(V是图的顶点集合)
实现:
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 二分图三·二分图最小点覆盖和最大独立集
标签:独立 out set code pre bool 最小边覆盖 int 最大
原文地址:https://www.cnblogs.com/wangyiming/p/9862550.html