POJ-3041.cpp 783 B

1234567891011121314151617181920212223
  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 n, k; cin >> n >> k;
  8. vector<vector<int>> g(n);
  9. while (k--) { int row, column; cin >> row >> column; g[row - 1].push_back(column - 1); }
  10. vector<int> match(n, -1), seen(n); int token = 0, answer = 0;
  11. auto augment = [&](auto &&self, int row) -> bool {
  12. for (int column : g[row]) if (seen[column] != token) {
  13. seen[column] = token;
  14. if (match[column] == -1 || self(self, match[column])) {
  15. match[column] = row; return true;
  16. }
  17. }
  18. return false;
  19. };
  20. for (int row = 0; row < n; ++row) { ++token; answer += augment(augment, row); }
  21. cout << answer << '\n';
  22. }