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)