Advertisement
matheus_monteiro

Swap Free

Dec 26th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define vi vector<int>
  5. #define SZ(a) (int)a.size()
  6. #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  7.  
  8. vector<int> AL[1005], graph[1005];
  9. vi match, vis, cor;
  10. int tempo;
  11.  
  12. void dfs(int v, int c) {
  13.     cor[v] = c;
  14.     for(int u : graph[v])
  15.         if(!cor[u])
  16.             dfs(u, 3 - c);
  17. }
  18.  
  19. bool is_swap_free(string a, string b) {
  20.     vector<int> p;
  21.     for(int i = 0; i < SZ(a); ++i)
  22.         if(a[i] != b[i])
  23.             p.push_back(i);
  24.     if(p.size() != 2) return true;
  25.     swap(a[p[0]], a[p[1]]);
  26.     return a == b ? false : true;
  27. }
  28.  
  29. int Aug(int L) {
  30.     if(vis[L] == tempo) return 0;
  31.     vis[L] = tempo;
  32.     for(int &R : AL[L]) {
  33.         if((match[R] == -1) or Aug(match[R])) {
  34.             match[R] = L;
  35.             return 1;
  36.         }
  37.     }
  38.     return 0;
  39. }
  40.  
  41. int32_t main() {
  42.    fastio;
  43.  
  44.    int n;
  45.    cin >> n;
  46.  
  47.    vector<string> arr(n);
  48.    for(string &w : arr) cin >> w;
  49.  
  50.    for(int i = 1; i <= n; ++i)
  51.         for(int j = i + 1; j <= n; ++j)
  52.             if(!is_swap_free(arr[i - 1], arr[j - 1])) {
  53.                 graph[i].push_back(j);
  54.                 graph[j].push_back(i);
  55.             }
  56.    
  57.     cor.resize(n + 1, 0);
  58.  
  59.     for(int i = 1; i <= n; ++i)
  60.         if(cor[i] == 0)
  61.             dfs(i, 1);
  62.    
  63.     for(int i = 1; i <= n; ++i)
  64.         for(int j : graph[i])
  65.             if(cor[i] == 1 and cor[j] == 2)
  66.                 AL[i].push_back(j);
  67.  
  68.     vis.resize(n + 1, 0);
  69.     match.resize(n + 1, -1);
  70.  
  71.     int match_cardinality = 0;
  72.  
  73.     for(int i = 1; i <= n; ++i)
  74.         if(cor[i] == 1) {
  75.             tempo++;
  76.             match_cardinality += Aug(i);
  77.         }
  78.     cout << n - match_cardinality << '\n';
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement