Poj 1466 girls and boys (Bipartite Graph Matching + split point + maximum independent set)

Source: Internet
Author: User
Poj 1466 girls and boys

Question Link

Question: N people, each of whom has a favorite set. Now I want to pick out some people so that no one in the collection will love each other and ask the maximum number of people in the collection.

Idea: each person is divided into two points. They love and are loved. Then they build a graph and run the bipartite graph to match the maximum. because the relationship between the two is mutual, the matching count will be twice more, then the number of people n-the maximum number of matches/2 is the maximum independent set

Code:

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N = 505;int n;vector<int> g[N];int left[N], vis[N];bool dfs(int u) {for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (vis[v]) continue;vis[v] = 1;if (left[v] == -1 || dfs(left[v])) {left[v] = u;return true;}}return false;}int hungary() {int ans = 0;memset(left, -1, sizeof(left));for (int i = 0; i < n; i++) {memset(vis, 0, sizeof(vis));if (dfs(i)) ans++;}return ans;}int main() {while (~scanf("%d", &n)) {for (int i = 0; i < n; i++) g[i].clear();int u, cnt, v;for (int i = 0; i < n; i++) {scanf("%d: (%d)", &u, &cnt);while (cnt--) {scanf("%d", &v);g[u].push_back(v);}}printf("%d\n", n - hungary() / 2);}return 0;}


Poj 1466 girls and boys (Bipartite Graph Matching + split point + 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.