#include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; struct kraw { int a, resi; }; int n, m, q; int f(int x, int y) { return x * n + y + 1; } int Licz(vector< vector<int> >& a) { vector< vector<kraw> > G(n * n + 2); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { if (a[i][j] == 0 && a[k][j] == 1) { G[f(i, j)].push_back({f(k, j), 1}); G[f(k, j)].push_back({f(i, j), 0}); } if (a[i][j] == 0 && a[i][k] == 1) { G[f(i, j)].push_back({f(i, k), 1}); G[f(i, k)].push_back({f(i, j), 0}); } } } } vector<int> zer, jed; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (a[i][j] == 0) zer.push_back(f(i, j)); else jed.push_back(f(i, j)); } } for (int i = 0; i < zer.size(); ++i) { G[0].push_back({zer[i], 1}); G[zer[i]].push_back({0, 0}); } for (int i = 0; i < jed.size(); ++i) { G[jed[i]].push_back({n * n + 1, 1}); G[n * n + 1].push_back({jed[i], 0}); } int flow = 0; while (true) { vector<bool> odw(n * n + 2, false); vector<int> pop(n * n + 2, -1); queue<int> Q; Q.push(0); odw[0] = true; while (!Q.empty()) { int akt = Q.front(); Q.pop(); for (int i = 0; i < G[akt].size(); ++i) { if (!odw[G[akt][i].a] && G[akt][i].resi > 0) { Q.push(G[akt][i].a); odw[G[akt][i].a] = true; pop[G[akt][i].a] = akt; } } } if (!odw[n * n + 1]) break; int f = 1; vector<int> path; int akt = n * n + 1; while (akt != -1) { path.push_back(akt); akt = pop[akt]; } reverse(path.begin(), path.end()); for (int i = 0; i < path.size() - 1; ++i) for (int j = 0; j < G[path[i]].size(); ++j) if (G[path[i]][j].a == path[i + 1]) f = min(f, G[path[i]][j].resi); flow += f; for (int i = 0; i < path.size() - 1; ++i) { for (int j = 0; j < G[path[i]].size(); ++j) if (G[path[i]][j].a == path[i + 1]) G[path[i]][j].resi -= f; for (int j = 0; j < G[path[i + 1]].size(); ++j) if (G[path[i + 1]][j].a == path[i]) G[path[i + 1]][j].resi += f; } } return flow; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> q; vector< vector<int> > a(n, vector<int>(n, 0)); for (int i = 0; i < m; ++i) { int x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; --x1; --x2; --y1; --y2; a[x1][y1] ^= 1; if (y2 + 1 < n) a[x1][y2 + 1] ^= 1; if (x2 + 1 < n) a[x2 + 1][y1] ^= 1; if (x2 + 1 < n && y2 + 1 < n) a[x2 + 1][y2 + 1] ^= 1; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 0) a[i][j] ^= a[i - 1][j]; if (j != 0) a[i][j] ^= a[i][j - 1]; if (i != 0 && j != 0) a[i][j] ^= a[i - 1][j - 1]; } } cout << Licz(a) << endl; for (int i = 0; i < q; ++i) { int x, y; cin >> x >> y; --x; --y; a[x][y] ^= 1; cout << Licz(a) << 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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; struct kraw { int a, resi; }; int n, m, q; int f(int x, int y) { return x * n + y + 1; } int Licz(vector< vector<int> >& a) { vector< vector<kraw> > G(n * n + 2); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { if (a[i][j] == 0 && a[k][j] == 1) { G[f(i, j)].push_back({f(k, j), 1}); G[f(k, j)].push_back({f(i, j), 0}); } if (a[i][j] == 0 && a[i][k] == 1) { G[f(i, j)].push_back({f(i, k), 1}); G[f(i, k)].push_back({f(i, j), 0}); } } } } vector<int> zer, jed; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (a[i][j] == 0) zer.push_back(f(i, j)); else jed.push_back(f(i, j)); } } for (int i = 0; i < zer.size(); ++i) { G[0].push_back({zer[i], 1}); G[zer[i]].push_back({0, 0}); } for (int i = 0; i < jed.size(); ++i) { G[jed[i]].push_back({n * n + 1, 1}); G[n * n + 1].push_back({jed[i], 0}); } int flow = 0; while (true) { vector<bool> odw(n * n + 2, false); vector<int> pop(n * n + 2, -1); queue<int> Q; Q.push(0); odw[0] = true; while (!Q.empty()) { int akt = Q.front(); Q.pop(); for (int i = 0; i < G[akt].size(); ++i) { if (!odw[G[akt][i].a] && G[akt][i].resi > 0) { Q.push(G[akt][i].a); odw[G[akt][i].a] = true; pop[G[akt][i].a] = akt; } } } if (!odw[n * n + 1]) break; int f = 1; vector<int> path; int akt = n * n + 1; while (akt != -1) { path.push_back(akt); akt = pop[akt]; } reverse(path.begin(), path.end()); for (int i = 0; i < path.size() - 1; ++i) for (int j = 0; j < G[path[i]].size(); ++j) if (G[path[i]][j].a == path[i + 1]) f = min(f, G[path[i]][j].resi); flow += f; for (int i = 0; i < path.size() - 1; ++i) { for (int j = 0; j < G[path[i]].size(); ++j) if (G[path[i]][j].a == path[i + 1]) G[path[i]][j].resi -= f; for (int j = 0; j < G[path[i + 1]].size(); ++j) if (G[path[i + 1]][j].a == path[i]) G[path[i + 1]][j].resi += f; } } return flow; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> q; vector< vector<int> > a(n, vector<int>(n, 0)); for (int i = 0; i < m; ++i) { int x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; --x1; --x2; --y1; --y2; a[x1][y1] ^= 1; if (y2 + 1 < n) a[x1][y2 + 1] ^= 1; if (x2 + 1 < n) a[x2 + 1][y1] ^= 1; if (x2 + 1 < n && y2 + 1 < n) a[x2 + 1][y2 + 1] ^= 1; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i != 0) a[i][j] ^= a[i - 1][j]; if (j != 0) a[i][j] ^= a[i][j - 1]; if (i != 0 && j != 0) a[i][j] ^= a[i - 1][j - 1]; } } cout << Licz(a) << endl; for (int i = 0; i < q; ++i) { int x, y; cin >> x >> y; --x; --y; a[x][y] ^= 1; cout << Licz(a) << endl; } return 0; } |