二分图匹配模板题
#include <bits/stdc++.h> #define FOPI freopen("in.txt", "r", stdin); #define FOPO freopen("out.txt", "w", stdout); using namespace std; typedef long long LL; const int maxn = 1500 + 1000; int n, k, x, y; int link[maxn], vis[maxn]; vector<int> v[maxn]; void build(int x, int y) { v[x].push_back(y), v[y].push_back(x); } bool dfs(int k) { int sz = v[k].size(); for (int i = 0; i < sz; i++) { if (!vis[v[k][i]]) { vis[v[k][i]] = 1; if (link[v[k][i]] == -1 || dfs(link[v[k][i]])) { link[v[k][i]] = k; return true; } } } return false; } int hungary() { int u; int res = 0; memset(link, -1, sizeof(link)); for (int i = 0; i < n; i++) { memset(vis, 0, sizeof(vis)); if (dfs(i)) res++; } return res; } int main() { while(~scanf("%d", &n)) { for (int i = 0; i < n; i++) v[i].clear(); for (int i = 1; i <= n; i++) { scanf("%d:(%d)", &x, &k); for (int j = 1; j <= k; j++) scanf("%d", &y), build(x, y), build(y, x); } printf("%d ", hungary()/2); } }