POJ-1274.cpp 881 B

123456789101112131415161718192021222324252627
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7. int cows, stalls;
  8. while (cin >> cows >> stalls) {
  9. vector<vector<int>> g(cows);
  10. for (int u = 0; u < cows; ++u) {
  11. int count; cin >> count;
  12. while (count--) { int v; cin >> v; g[u].push_back(v - 1); }
  13. }
  14. vector<int> match(stalls, -1), seen(stalls);
  15. int token = 0, answer = 0;
  16. auto augment = [&](auto &&self, int u) -> bool {
  17. for (int v : g[u]) if (seen[v] != token) {
  18. seen[v] = token;
  19. if (match[v] == -1 || self(self, match[v])) { match[v] = u; return true; }
  20. }
  21. return false;
  22. };
  23. for (int u = 0; u < cows; ++u) { ++token; answer += augment(augment, u); }
  24. cout << answer << '\n';
  25. }
  26. }