#include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define _ ios_base::sync_with_stdio(0); cin.tie(0); //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 2000 + 10; int n,m,q; bool t[nax][nax]; vi G[nax * nax]; bool vis[nax * nax]; int match[nax * nax]; bool path(int x) { vis[x] = 1; for(int nbh : G[x]) { if(match[nbh] == -1) { match[x] = nbh; match[nbh] = x; return 1; } } for(int nbh : G[x]) { if(!vis[match[nbh]] && path(match[nbh])) { match[x] = nbh; match[nbh] = x; return 1; } } return 0; } int main() {_ cin >> n >> m >> q; for(int a,b,c,d,i=1; i <= m; ++i) { cin >> a >> b >> c >> d; t[a][b] ^= 1; t[c + 1][b] ^= 1; t[a][d + 1] ^= 1; t[c + 1][d + 1] ^= 1; } for(int i = 1; i <= n; ++i) { for(int j = 1; j <= n; ++j) { t[i][j] ^= t[i - 1][j] ^ t[i][j - 1] ^ t[i - 1][j - 1]; } } for(int i = 1; i <= n; ++i) { for(int j = 1; j <= n; ++j) { for(int k = 1; k <= n; ++k) { if(k != j && t[i][k] != t[i][j]) { G[(i-1)*n + (j-1)].PB((i-1)*n+(k-1)); } if(k != i && t[k][j] != t[i][j]) { G[(i - 1)*n +(j-1)].PB((k-1)*n+(j-1)); } } } } int ans = 0; for(int i = 0; i < n * n; ++i) match[i] = -1; for(int i = 1; i <= n; ++i) { vi w = {}, b = {}; for(int j = 1; j <= n; ++j) { if(t[i][j]) b.PB(j); else w.PB(j); } for(int j = 0; j < min((int)w.size(), (int)b.size()); ++j) { match[(i - 1) * n + w[j]-1] = (i - 1) * n + b[j] - 1; match[(i - 1) * n + b[j]-1] = (i - 1) * n + w[j] - 1; } } while(1) { for(int i = 0; i < n * n; ++i) vis[i] = 0; bool any = 0; for(int i = 0; i < n * n; ++i) { if(match[i] == -1 && path(i)) any = 1; } if(!any) break; } for(int i = 0; i < n * n; ++i) { if(match[i] != -1) ans++; } ans /= 2; cout << ans << "\n"; for(int ii = 1; ii <= q; ++ii) { int a, b; cin >> a >> b; G[(a - 1) * n + (b - 1)].clear(); for(int i = 1; i <= n; ++i) { if(i != a) { if(t[i][b] == t[a][b]) { G[(i - 1)*n+(b-1)].PB((a-1)*n+(b-1)); G[(a-1)*n+(b-1)].PB((i-1)*n+(b-1)); } else { int he = (i-1)*n+(b-1); int me = (a-1)*n+(b-1); for(int j = 0; j < (int)G[he].size(); ++j) { if(G[he][j] == me) { swap(G[he][j], G[he].back()); G[he].pop_back(); break; } } } } if(i != b) { if(t[a][i] == t[a][b]) { G[(a-1)*n+i-1].PB((a-1)*n+(b-1)); G[(a-1)*n+(b-1)].PB((a-1)*n+i-1); } else { int me = (a-1)*n+(b-1); int he = (a-1)*n+(i-1); for(int j = 0; j < (int)G[he].size(); ++j) { if(G[he][j] == me) { swap(G[he][j], G[he].back()); G[he].pop_back(); break; } } } } } t[a][b] = 1 - t[a][b]; int me = (a - 1) * n + (b - 1), he = -1; if(match[me] != -1) { ans--; he = match[me]; match[match[me]] = -1; match[me] = -1; } while(1) { for(int i = 0; i < n * n; ++i) vis[i] = 0; bool any = 0; for(int i = 0; i < n * n; ++i) { if(match[i] == -1 && path(i)) { any = 1; ans++; break; } } if(!any) break; } cout << ans << "\n"; } }
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 | #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second #define _ ios_base::sync_with_stdio(0); cin.tie(0); //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 2000 + 10; int n,m,q; bool t[nax][nax]; vi G[nax * nax]; bool vis[nax * nax]; int match[nax * nax]; bool path(int x) { vis[x] = 1; for(int nbh : G[x]) { if(match[nbh] == -1) { match[x] = nbh; match[nbh] = x; return 1; } } for(int nbh : G[x]) { if(!vis[match[nbh]] && path(match[nbh])) { match[x] = nbh; match[nbh] = x; return 1; } } return 0; } int main() {_ cin >> n >> m >> q; for(int a,b,c,d,i=1; i <= m; ++i) { cin >> a >> b >> c >> d; t[a][b] ^= 1; t[c + 1][b] ^= 1; t[a][d + 1] ^= 1; t[c + 1][d + 1] ^= 1; } for(int i = 1; i <= n; ++i) { for(int j = 1; j <= n; ++j) { t[i][j] ^= t[i - 1][j] ^ t[i][j - 1] ^ t[i - 1][j - 1]; } } for(int i = 1; i <= n; ++i) { for(int j = 1; j <= n; ++j) { for(int k = 1; k <= n; ++k) { if(k != j && t[i][k] != t[i][j]) { G[(i-1)*n + (j-1)].PB((i-1)*n+(k-1)); } if(k != i && t[k][j] != t[i][j]) { G[(i - 1)*n +(j-1)].PB((k-1)*n+(j-1)); } } } } int ans = 0; for(int i = 0; i < n * n; ++i) match[i] = -1; for(int i = 1; i <= n; ++i) { vi w = {}, b = {}; for(int j = 1; j <= n; ++j) { if(t[i][j]) b.PB(j); else w.PB(j); } for(int j = 0; j < min((int)w.size(), (int)b.size()); ++j) { match[(i - 1) * n + w[j]-1] = (i - 1) * n + b[j] - 1; match[(i - 1) * n + b[j]-1] = (i - 1) * n + w[j] - 1; } } while(1) { for(int i = 0; i < n * n; ++i) vis[i] = 0; bool any = 0; for(int i = 0; i < n * n; ++i) { if(match[i] == -1 && path(i)) any = 1; } if(!any) break; } for(int i = 0; i < n * n; ++i) { if(match[i] != -1) ans++; } ans /= 2; cout << ans << "\n"; for(int ii = 1; ii <= q; ++ii) { int a, b; cin >> a >> b; G[(a - 1) * n + (b - 1)].clear(); for(int i = 1; i <= n; ++i) { if(i != a) { if(t[i][b] == t[a][b]) { G[(i - 1)*n+(b-1)].PB((a-1)*n+(b-1)); G[(a-1)*n+(b-1)].PB((i-1)*n+(b-1)); } else { int he = (i-1)*n+(b-1); int me = (a-1)*n+(b-1); for(int j = 0; j < (int)G[he].size(); ++j) { if(G[he][j] == me) { swap(G[he][j], G[he].back()); G[he].pop_back(); break; } } } } if(i != b) { if(t[a][i] == t[a][b]) { G[(a-1)*n+i-1].PB((a-1)*n+(b-1)); G[(a-1)*n+(b-1)].PB((a-1)*n+i-1); } else { int me = (a-1)*n+(b-1); int he = (a-1)*n+(i-1); for(int j = 0; j < (int)G[he].size(); ++j) { if(G[he][j] == me) { swap(G[he][j], G[he].back()); G[he].pop_back(); break; } } } } } t[a][b] = 1 - t[a][b]; int me = (a - 1) * n + (b - 1), he = -1; if(match[me] != -1) { ans--; he = match[me]; match[match[me]] = -1; match[me] = -1; } while(1) { for(int i = 0; i < n * n; ++i) vis[i] = 0; bool any = 0; for(int i = 0; i < n * n; ++i) { if(match[i] == -1 && path(i)) { any = 1; ans++; break; } } if(!any) break; } cout << ans << "\n"; } } |