#include <bits/stdc++.h> #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<int> #define pi pair<int, int> #define MAXN 100001 using namespace std; int count(vector<vector<bool>> &A) { int res = 0; for (int i = 0; i < A.size(); i++) { for (int j = 0; j < A[i].size(); j++) { if (A[i][j]) res++; cout << A[i][j] << " "; } cout << endl; } return res; } int gidx(int x, int y, int n) { return x * n + y; } pi rgidx(int idx, int n) { return make_pair(idx / n, idx % n); } void make_graph(vector<vector<bool>> &A, vector<vi> &V, vi &M, vector<bool> &B, bool f) { int n = A.size(); for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { int idx = gidx(x, y, n); if (f) { V[idx].clear(); M[idx] = -1; B[idx] = false; } if (!A[x][y]) continue; for (int i = 0; i < n; i++) { if (!A[i][y]) { int ne = gidx(i, y, n); V[idx].pb(ne); V[ne].pb(idx); } if (!A[x][i]) { int ne = gidx(x, i, n); V[idx].pb(ne); V[ne].pb(idx); } } } } } bool dfs(int x, int p, vector<vi> &V, vi &M, vector<bool> &B) { if (B[x]) return false; B[x] = true; for (int i = 0; i < V[x].size(); i++) { int y = V[x][i]; if (y != p && (M[y] == -1 || dfs(M[y], y, V, M, B) == true)) { M[x] = y; M[y] = x; return true; } } return false; } int matching(int n, vector<vector<bool>> &A, vector<vi> &V, vi &M, vector<bool> &B) { bool c = true; while (c) { c = false; fill(B.begin(), B.end(), false); for (int i = 0; i < n * n; i++) if (M[i] == -1 && !B[i]) c = max(dfs(i, -1, V, M, B), c); } int counter = 0; for (int i = 0; i < n * n; i++) { pi xy = rgidx(i, n); if (A[xy.fi][xy.se] && M[i] != -1) counter++; } return counter; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<bool>> A(n); for (int i = 0; i < n; i++) { A[i].resize(n); fill(A[i].begin(), A[i].end(), false); } int x1, x2, y1, y2; for (int i = 0; i < m; i++) { cin >> x1 >> y1 >> x2 >> y2; for (int x = x1 - 1; x < x2; x++) { for (int y = y1 - 1; y < y2; y++) { A[x][y] = !A[x][y]; } } } vector<vi> V(n * n); vi M(n * n); vector<bool> B(n * n); make_graph(A, V, M, B, true); int c = matching(n, A, V, M, B); cout << c << endl; // for (int x = 0; x < n; x++) { // for (int y = 0; y < n; y++) { // cout << A[x][y] << " "; // } // cout << endl; // } // for (int x = 0; x < n; x++) { // for (int y = 0; y < n; y++) { // cout << M[gidx(x, y, n)] << " "; // } // cout << endl; // } for (int i = 0; i < q; i++) { cin >> x1 >> y1; x1--; y1--; A[x1][y1] = !A[x1][y1]; int idx = gidx(x1, y1, n); B[idx] = false; if (M[idx] != -1) { int mate = M[idx]; M[idx] = M[mate] = -1; B[mate] = false; } make_graph(A, V, M, B, false); cout << matching(n, A, V, M, B) << endl; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <bits/stdc++.h> #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fi first #define se second #define ll long long #define ld long double #define pb push_back #define vi vector<int> #define pi pair<int, int> #define MAXN 100001 using namespace std; int count(vector<vector<bool>> &A) { int res = 0; for (int i = 0; i < A.size(); i++) { for (int j = 0; j < A[i].size(); j++) { if (A[i][j]) res++; cout << A[i][j] << " "; } cout << endl; } return res; } int gidx(int x, int y, int n) { return x * n + y; } pi rgidx(int idx, int n) { return make_pair(idx / n, idx % n); } void make_graph(vector<vector<bool>> &A, vector<vi> &V, vi &M, vector<bool> &B, bool f) { int n = A.size(); for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { int idx = gidx(x, y, n); if (f) { V[idx].clear(); M[idx] = -1; B[idx] = false; } if (!A[x][y]) continue; for (int i = 0; i < n; i++) { if (!A[i][y]) { int ne = gidx(i, y, n); V[idx].pb(ne); V[ne].pb(idx); } if (!A[x][i]) { int ne = gidx(x, i, n); V[idx].pb(ne); V[ne].pb(idx); } } } } } bool dfs(int x, int p, vector<vi> &V, vi &M, vector<bool> &B) { if (B[x]) return false; B[x] = true; for (int i = 0; i < V[x].size(); i++) { int y = V[x][i]; if (y != p && (M[y] == -1 || dfs(M[y], y, V, M, B) == true)) { M[x] = y; M[y] = x; return true; } } return false; } int matching(int n, vector<vector<bool>> &A, vector<vi> &V, vi &M, vector<bool> &B) { bool c = true; while (c) { c = false; fill(B.begin(), B.end(), false); for (int i = 0; i < n * n; i++) if (M[i] == -1 && !B[i]) c = max(dfs(i, -1, V, M, B), c); } int counter = 0; for (int i = 0; i < n * n; i++) { pi xy = rgidx(i, n); if (A[xy.fi][xy.se] && M[i] != -1) counter++; } return counter; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<bool>> A(n); for (int i = 0; i < n; i++) { A[i].resize(n); fill(A[i].begin(), A[i].end(), false); } int x1, x2, y1, y2; for (int i = 0; i < m; i++) { cin >> x1 >> y1 >> x2 >> y2; for (int x = x1 - 1; x < x2; x++) { for (int y = y1 - 1; y < y2; y++) { A[x][y] = !A[x][y]; } } } vector<vi> V(n * n); vi M(n * n); vector<bool> B(n * n); make_graph(A, V, M, B, true); int c = matching(n, A, V, M, B); cout << c << endl; // for (int x = 0; x < n; x++) { // for (int y = 0; y < n; y++) { // cout << A[x][y] << " "; // } // cout << endl; // } // for (int x = 0; x < n; x++) { // for (int y = 0; y < n; y++) { // cout << M[gidx(x, y, n)] << " "; // } // cout << endl; // } for (int i = 0; i < q; i++) { cin >> x1 >> y1; x1--; y1--; A[x1][y1] = !A[x1][y1]; int idx = gidx(x1, y1, n); B[idx] = false; if (M[idx] != -1) { int mate = M[idx]; M[idx] = M[mate] = -1; B[mate] = false; } make_graph(A, V, M, B, false); cout << matching(n, A, V, M, B) << endl; } return 0; } |