#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <climits>
using namespace std;
void reinit(int &n, vector<vector<int>> &arr, vector<vector<int>> &G, bool flag) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (flag && !G[(i - 1) * n + j].empty()) G[(i - 1) * n + j].clear();
if (arr[i][j] == 1) {
for (int h = 1; h <= n; h++) {
if (h != j && arr[i][h] == 0) G[(i - 1) * n + j].push_back((i - 1) * n + h);
if (h != i && arr[h][j] == 0) G[(i - 1) * n + j].push_back((h - 1) * n + j);
}
}
}
}
}
void input(int &n, int &m, int &q, vector<vector<int>> &arr, vector<vector<int>> &G) {
int x1, x2, y1, y2;
cin >> n >> m >> q;
arr.resize(n + 2, vector<int>(n + 2, 0));
G.resize(n * n + 3);
for (int t = 0; t < m; t++) {
cin >> x1 >> y1 >> x2 >> y2;
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
arr[i][j] = (arr[i][j] + 1) % 2;
}
}
}
reinit(n, arr, G, false);
}
bool bfs(int &n, vector<vector<int>> &G, vector<int> &dist, vector<int> &match) {
queue<int> q;
for (int i = 1; i <= n; i++) {
if (match[i] == 0) {
dist[i] = 0;
q.push(i);
}
else dist[i] = INT_MAX;
}
dist[0] = INT_MAX;
while(!q.empty()) {
int u = q.front();
q.pop();
if (u != 0) {
int len = G[u].size();
for (int v : G[u]) {
if (dist[match[v]] == INT_MAX) {
dist[match[v]] = dist[u] + 1;
q.push(match[v]);
}
}
}
}
return dist[0] != INT_MAX;
}
bool dfs(int u, vector<vector<int>> &G, vector<int> &dist, vector<int> &match) {
if (u == 0) return true;
for (int v : G[u]) {
if (dist[match[v]] == dist[u] + 1 && dfs(match[v], G, dist, match)) {
match[v] = u;
match[u] = v;
return true;
}
}
dist[u] = INT_MAX;
return false;
}
//Hopcroft-Kraft: http://zobayer.blogspot.com/2010/05/maximum-matching.html
int hcp(int n, vector<vector<int>> &G, vector<int> &dist, vector<int> &match) {
int result = 0;
while (bfs(n, G, dist, match)) {
for (int i = 1; i <= n; i++) {
if (match[i] == 0 && dfs(i, G, dist, match)) result++;
}
}
return result;
}
int main() {
ios_base::sync_with_stdio(0);
int n, m, q, a, b;
vector<vector<int>> arr, G;
input(n, m, q, arr, G);
vector<int> dist(n * n + 3), match(n * n + 3);
cout << hcp(n * n, G, dist, match) << "\n";
while (q--) {
for (int i = 0; i <= n * n; i++) {
dist[i] = 0;
match[i] = 0;
}
cin >> a >> b;
arr[a][b] = (arr[a][b] + 1) % 2;
reinit(n, arr, G, true);
cout << hcp(n * n, G, dist, match) << "\n";
}
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 | #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <deque> #include <climits> using namespace std; void reinit(int &n, vector<vector<int>> &arr, vector<vector<int>> &G, bool flag) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (flag && !G[(i - 1) * n + j].empty()) G[(i - 1) * n + j].clear(); if (arr[i][j] == 1) { for (int h = 1; h <= n; h++) { if (h != j && arr[i][h] == 0) G[(i - 1) * n + j].push_back((i - 1) * n + h); if (h != i && arr[h][j] == 0) G[(i - 1) * n + j].push_back((h - 1) * n + j); } } } } } void input(int &n, int &m, int &q, vector<vector<int>> &arr, vector<vector<int>> &G) { int x1, x2, y1, y2; cin >> n >> m >> q; arr.resize(n + 2, vector<int>(n + 2, 0)); G.resize(n * n + 3); for (int t = 0; t < m; t++) { cin >> x1 >> y1 >> x2 >> y2; for (int i = x1; i <= x2; i++) { for (int j = y1; j <= y2; j++) { arr[i][j] = (arr[i][j] + 1) % 2; } } } reinit(n, arr, G, false); } bool bfs(int &n, vector<vector<int>> &G, vector<int> &dist, vector<int> &match) { queue<int> q; for (int i = 1; i <= n; i++) { if (match[i] == 0) { dist[i] = 0; q.push(i); } else dist[i] = INT_MAX; } dist[0] = INT_MAX; while(!q.empty()) { int u = q.front(); q.pop(); if (u != 0) { int len = G[u].size(); for (int v : G[u]) { if (dist[match[v]] == INT_MAX) { dist[match[v]] = dist[u] + 1; q.push(match[v]); } } } } return dist[0] != INT_MAX; } bool dfs(int u, vector<vector<int>> &G, vector<int> &dist, vector<int> &match) { if (u == 0) return true; for (int v : G[u]) { if (dist[match[v]] == dist[u] + 1 && dfs(match[v], G, dist, match)) { match[v] = u; match[u] = v; return true; } } dist[u] = INT_MAX; return false; } //Hopcroft-Kraft: http://zobayer.blogspot.com/2010/05/maximum-matching.html int hcp(int n, vector<vector<int>> &G, vector<int> &dist, vector<int> &match) { int result = 0; while (bfs(n, G, dist, match)) { for (int i = 1; i <= n; i++) { if (match[i] == 0 && dfs(i, G, dist, match)) result++; } } return result; } int main() { ios_base::sync_with_stdio(0); int n, m, q, a, b; vector<vector<int>> arr, G; input(n, m, q, arr, G); vector<int> dist(n * n + 3), match(n * n + 3); cout << hcp(n * n, G, dist, match) << "\n"; while (q--) { for (int i = 0; i <= n * n; i++) { dist[i] = 0; match[i] = 0; } cin >> a >> b; arr[a][b] = (arr[a][b] + 1) % 2; reinit(n, arr, G, true); cout << hcp(n * n, G, dist, match) << "\n"; } return 0; } |
English