Advertisement
Vince14

/<> 1671 (maximum bipartite matching)

Oct 28th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. using namespace std;
  16. #define pii pair<int , int>
  17. #define ll long long
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  19. const long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};
  20. const long long dl[2] = {1, -1};
  21. const long long MOD = 1000000007;
  22. const long long MAXN = 55;
  23.  
  24. int n;
  25. pair<int, pii> arr[MAXN];
  26. vector<int> v[MAXN];
  27. vector<pii> shark;
  28. vector<bool> visited;
  29. vector<int> prey;
  30.  
  31. void init(){
  32.     cin >> n;
  33.     for(int x, y, z, i = 0; i < n; i++){
  34.         cin >> arr[i].first >> arr[i].second.first >> arr[i].second.second;
  35.     }
  36.     for(int i = 0; i < n; i++){
  37.         for(int j = 0; j < n; j++){
  38.             if(i == j) continue;
  39.             if(arr[i].first >= arr[j].first && arr[i].second.first >= arr[j].second.first && arr[i].second.second >= arr[j].second.second){
  40.                 v[i].push_back(j);
  41.             }
  42.         }
  43.     }
  44.     for(int i = 0; i < n; i++){
  45.         shark.push_back({(int) v[i].size(), i});
  46.     }
  47.     sort(shark.begin(), shark.end());
  48. }
  49.  
  50. bool dfs(int cur){
  51.     if(visited[cur]){
  52.         return false;
  53.     }
  54.     visited[cur] = true;
  55.     for(auto nxt : v[cur]){
  56.         if(nxt == cur || (nxt > cur && arr[nxt] == arr[cur])) continue;
  57.         if(prey[nxt] == -1 || dfs(prey[nxt])){
  58.             prey[nxt] = cur;
  59.             return true;
  60.         }
  61.     }
  62.     return false;
  63. }
  64.  
  65. int solve(){
  66.     int ans = 0;
  67.     prey.assign(n, -1);
  68.     for(pii s : shark){
  69.         visited.assign(n, false);
  70.         if(dfs(s.second)){
  71.             ans++;
  72.         }
  73.         visited.assign(n, false);
  74.         if(dfs(s.second)){
  75.             ans++;
  76.         }
  77.     }
  78.     return max(n - ans, 1);
  79. }
  80.  
  81.  
  82. int main() {
  83.     FAST;
  84.     init();
  85.     cout << solve();
  86. }
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement