#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define PPC(x) __builtin_popcount(x) #define MSB(x) (63 - __builtin_clzll(x)) #define SZ(x) ((int)(x).size()) #define HASK(S, x) (S.find(x) != S.end()) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ithBit(m, i) ((m) >> (i) & 1) #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 23, maxD = 1 << 11; namespace Matching { int *part, n, m, visctr, ser, dist[maxN], vis[maxN]; int qb, Q[maxN]; vector <int>* G; void ins(int v, int d) { dist[v] = d; vis[v] = visctr; if (d <= ser) Q[qb++] = v; } bool bfs(const vector <int>& hint = {}) { visctr++; ser = n + m; qb = 0; if (hint.empty()) { FOR(i, 1, n+1) if (part[i] == 0) ins(i, 0); } else for (int v : hint) if (part[v] == 0) ins(v, 0); for (int qf = 0; qf != qb; qf++) { int v = Q[qf]; for (int w : G[v]) { int u = part[w]; if (vis[u] == visctr) continue; if (u == 0) { ser = min(ser, dist[v]+1); continue; } ins(u, dist[v] + 2); } } visctr++; return ser != n + m; } void match(int a, int b) { part[a] = b, part[b] = a; } bool dfs(int v) { vis[v] = visctr; if (dist[v] > ser) return false; for (int w : G[v]) { int u = part[w]; if (u == 0 or (vis[u] != visctr and dist[u] == dist[v] + 2 and dfs(u))) { match(w, v); return true; } } return false; } int run(vector <int>* Gin, int nn, int mm, int* partout, const vector <int>& hint = {}) { G = Gin; n = nn; m = mm; part = partout; bfs(hint); for (int v : hint) if (part[v] == 0) dfs(v); while (bfs()) FOR(i, 1, n+1) if (part[i] == 0) dfs(i); int ret = 0; FOR(i, 1, n+1) ret += part[i] != 0; return ret; } }; int rect[maxD][maxD], suf[maxD][maxD], mp[maxD][maxD], part[maxN]; vector <int> G[maxN]; void flip(int i, int j) { rect[i][j] ^= 1; } void conn(int i, int j) { G[i].pb(j), G[j].pb(i); } void ers(vector <int>& t, int x) { for (int& v : t) if (v == x) { swap(t.back(), v); t.pop_back(); break; } } void solve() { int n, m, q; scanf ("%d%d%d", &n, &m, &q); while (m--) { int a, b, c, d; scanf ("%d%d%d%d", &a, &b, &c, &d); flip(a-1, b-1); flip(a-1, d); flip(c, b-1); flip(c, d); } FORD(i, n, 0) FORD(j, n, 0) suf[i][j] = suf[i+1][j] ^ suf[i][j+1] ^ suf[i+1][j+1] ^ rect[i][j]; int k = 0; FOR(i, 1, n+1) FOR(j, 1, n+1) { mp[i][j] = ++k; if (suf[i][j]) mp[i][j] += n*n; } FOR(i, 1, n+1) FOR(j1, 1, n+1) FOR(j2, 1, j1) if (suf[i][j1] != suf[i][j2]) conn(mp[i][j1], mp[i][j2]); FOR(j, 1, n+1) FOR(i1, 1, n+1) FOR(i2, 1, i1) if (suf[i1][j] != suf[i2][j]) conn(mp[i1][j], mp[i2][j]); int res = Matching::run(G, k, k, part); printf("%d\n", res); while (q--) { int i, j; scanf ("%d%d", &i, &j); int old = mp[i][j], nw = old <= k ? old + k : old - k; FOR(j2, 1, n+1) if (j2 != j) { int v = mp[i][j2]; if ((v <= k) xor (old <= k)) ers(G[v], old); else conn(v, nw); } FOR(i2, 1, n+1) if (i2 != i) { int v = mp[i2][j]; if ((v <= k) xor (old <= k)) ers(G[v], old); else conn(v, nw); } G[old].resize(0); vector <int> hint = {nw}; if (part[old] != 0) { hint.pb(part[old]); part[part[old]] = 0; part[old] = 0; res--; } mp[i][j] = nw; res = Matching::run(G, k, k, part, hint); printf("%d\n", res); } /* dbg(res); FOR(i, 1, n+1) { FOR(j, 1, n+1) { char c = '_'; int v = mp[i][j]; if (part[v] == 0) c = v <= k ? '.' : '#'; printf("%c", c); } printf("\n"); } */ } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (int i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define PPC(x) __builtin_popcount(x) #define MSB(x) (63 - __builtin_clzll(x)) #define SZ(x) ((int)(x).size()) #define HASK(S, x) (S.find(x) != S.end()) #define pb push_back #define ALL(x) (x).begin(), (x).end() #define ithBit(m, i) ((m) >> (i) & 1) #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 23, maxD = 1 << 11; namespace Matching { int *part, n, m, visctr, ser, dist[maxN], vis[maxN]; int qb, Q[maxN]; vector <int>* G; void ins(int v, int d) { dist[v] = d; vis[v] = visctr; if (d <= ser) Q[qb++] = v; } bool bfs(const vector <int>& hint = {}) { visctr++; ser = n + m; qb = 0; if (hint.empty()) { FOR(i, 1, n+1) if (part[i] == 0) ins(i, 0); } else for (int v : hint) if (part[v] == 0) ins(v, 0); for (int qf = 0; qf != qb; qf++) { int v = Q[qf]; for (int w : G[v]) { int u = part[w]; if (vis[u] == visctr) continue; if (u == 0) { ser = min(ser, dist[v]+1); continue; } ins(u, dist[v] + 2); } } visctr++; return ser != n + m; } void match(int a, int b) { part[a] = b, part[b] = a; } bool dfs(int v) { vis[v] = visctr; if (dist[v] > ser) return false; for (int w : G[v]) { int u = part[w]; if (u == 0 or (vis[u] != visctr and dist[u] == dist[v] + 2 and dfs(u))) { match(w, v); return true; } } return false; } int run(vector <int>* Gin, int nn, int mm, int* partout, const vector <int>& hint = {}) { G = Gin; n = nn; m = mm; part = partout; bfs(hint); for (int v : hint) if (part[v] == 0) dfs(v); while (bfs()) FOR(i, 1, n+1) if (part[i] == 0) dfs(i); int ret = 0; FOR(i, 1, n+1) ret += part[i] != 0; return ret; } }; int rect[maxD][maxD], suf[maxD][maxD], mp[maxD][maxD], part[maxN]; vector <int> G[maxN]; void flip(int i, int j) { rect[i][j] ^= 1; } void conn(int i, int j) { G[i].pb(j), G[j].pb(i); } void ers(vector <int>& t, int x) { for (int& v : t) if (v == x) { swap(t.back(), v); t.pop_back(); break; } } void solve() { int n, m, q; scanf ("%d%d%d", &n, &m, &q); while (m--) { int a, b, c, d; scanf ("%d%d%d%d", &a, &b, &c, &d); flip(a-1, b-1); flip(a-1, d); flip(c, b-1); flip(c, d); } FORD(i, n, 0) FORD(j, n, 0) suf[i][j] = suf[i+1][j] ^ suf[i][j+1] ^ suf[i+1][j+1] ^ rect[i][j]; int k = 0; FOR(i, 1, n+1) FOR(j, 1, n+1) { mp[i][j] = ++k; if (suf[i][j]) mp[i][j] += n*n; } FOR(i, 1, n+1) FOR(j1, 1, n+1) FOR(j2, 1, j1) if (suf[i][j1] != suf[i][j2]) conn(mp[i][j1], mp[i][j2]); FOR(j, 1, n+1) FOR(i1, 1, n+1) FOR(i2, 1, i1) if (suf[i1][j] != suf[i2][j]) conn(mp[i1][j], mp[i2][j]); int res = Matching::run(G, k, k, part); printf("%d\n", res); while (q--) { int i, j; scanf ("%d%d", &i, &j); int old = mp[i][j], nw = old <= k ? old + k : old - k; FOR(j2, 1, n+1) if (j2 != j) { int v = mp[i][j2]; if ((v <= k) xor (old <= k)) ers(G[v], old); else conn(v, nw); } FOR(i2, 1, n+1) if (i2 != i) { int v = mp[i2][j]; if ((v <= k) xor (old <= k)) ers(G[v], old); else conn(v, nw); } G[old].resize(0); vector <int> hint = {nw}; if (part[old] != 0) { hint.pb(part[old]); part[part[old]] = 0; part[old] = 0; res--; } mp[i][j] = nw; res = Matching::run(G, k, k, part, hint); printf("%d\n", res); } /* dbg(res); FOR(i, 1, n+1) { FOR(j, 1, n+1) { char c = '_'; int v = mp[i][j]; if (part[v] == 0) c = v <= k ? '.' : '#'; printf("%c", c); } printf("\n"); } */ } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; } |