// Implementacja algorytmu Hopcrofta Karpa na podstawie ksiazki Algorytmika Praktyczna #include <cstdio> #include <algorithm> #include <vector> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second template<class V, class E> struct Graph { struct Ed : E { int v; Ed(E p, int w) : E(p), v(w) {} }; struct Ve : V,vector<Ed> {}; vector<Ve> g; Graph(int n=0) : g(n) {} void EdgeD(int b, int e, E d = E()) {g[b].PB(Ed(d,e));} int topo; void TopoDfs(int v){ if (!g[v].t) { g[v].t=1; FOREACH(it,g[v]) TopoDfs(it->v); g[v].t=--topo; } } void TopoSort(){ FOREACH(it,g) it->t=0; topo=SIZE(g); FORD(x,topo-1,0) TopoDfs(x); } VI TopoSortV(){ VI res(SIZE(g)); TopoSort(); REP(x,SIZE(g)) res[g[x].t] = x; return res; } void Bfs(int s) { FOREACH(it, g) it->t=it->s=-1; g[s].t=0; int qu[SIZE(g)],b,e; qu[b=e=0]=s; while(b<=e) { s=qu[b++]; FOREACH(it, g[s]) if (g[it->v].t==-1) { g[qu[++e]=it->v].t=g[s].t+1; g[it->v].s=s; } } } void mvFlow(int v, Ed &e){ int u=e.v; g[u].PB(e); g[u].back().v=v; swap(g[v].back(),e); g[v].pop_back(); } int Ue; bool UFDfs(int v){ if (v==Ue) return true; g[v].s=1; FOREACH(it,g[v]) if (g[it->v].t==1+g[v].t && !g[it->v].s && UFDfs(it->v)){ mvFlow(v,*it); return true; } return false; } int UnitFlow(int v1,int v2){ int res=0; Ue=v2; while (1){ Bfs(v1); if (g[v2].t==-1) break; FOREACH(it,g) it->s=0; FOREACH(it,g[v1]) if (UFDfs(it->v)) {res++; mvFlow(v1, *it--);} } return res; } VI Hopcroft(){ int n=SIZE(g); VI res(n,-1); vector<char> l; if (!BiPart(l)) return res; g.resize(n+2); REP(i,n) if (!l[i]) EdgeD(n,i); else EdgeD(i,n+1); UnitFlow(n,n+1); REP(i,n) if (l[i] && g[i][0].v!=n+1) res[ res[g[i][0].v]=i ]=g[i][0].v; return res; } bool BiPart(vector<char> &v) { v.resize(SIZE(g),2); VI r = TopoSortV(); FOREACH(x, r) { if(v[*x]==2) v[*x]=0; FOREACH(it, g[*x]) if (v[it->v]==2) v[it->v] = 1-v[*x]; else if (v[it->v] == v[*x]) return 0; } return 1; } }; struct Ve {}; struct Vs {int t, s;}; int tab_rows[2002][2002]; void first_query(int n) { Graph<Vs, Ve> g(n * n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (tab_rows[i][j]) { for (int k = 0; k < n; k++) { if (!tab_rows[i][k]) { g.EdgeD(n * i + j, n * i + k); } if (!tab_rows[k][j]) { g.EdgeD(n * i + j, n * k + j); } } } } } VI res = g.Hopcroft(); int ret = 0; REP(x, SIZE(res)) if (res[x] > x) { ret++; } printf("%d\n", ret); } int main() { int n, m, q; scanf("%d %d %d", &n, &m, &q); for (int i = 0; i < m; i++) { int row1, row2, col1, col2; scanf("%d %d %d %d", &row1, &col1, &row2, &col2); --row1; --row2; --col1; --col2; for (int j = row1; j <= row2; j++) { tab_rows[j][col1]++; tab_rows[j][col2 + 1]--; } } for (int i = 0; i < n; i++) { int pref = 0; int s = 0; for (int j = 0; j < n; j++) { pref += tab_rows[i][j]; tab_rows[i][j] = pref % 2; } } first_query(n); for (int z = 0; z < q; z++) { int query_row, query_col; scanf("%d %d", &query_row, &query_col); --query_row; --query_col; if (tab_rows[query_row][query_col]) { tab_rows[query_row][query_col] = 0; } else { tab_rows[query_row][query_col] = 1; } Graph<Vs, Ve> g(n * n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (tab_rows[i][j]) { for (int k = 0; k < n; k++) { if (!tab_rows[i][k]) { g.EdgeD(n * i + j, n * i + k); } if (!tab_rows[k][j]) { g.EdgeD(n * i + j, n * k + j); } } } } } VI res = g.Hopcroft(); int ret =0; REP(x, SIZE(res)) if (res[x] > x) { ret++; } printf("%d\n", ret); } 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | // Implementacja algorytmu Hopcrofta Karpa na podstawie ksiazki Algorytmika Praktyczna #include <cstdio> #include <algorithm> #include <vector> using namespace std; typedef vector<int> VI; typedef long long LL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second template<class V, class E> struct Graph { struct Ed : E { int v; Ed(E p, int w) : E(p), v(w) {} }; struct Ve : V,vector<Ed> {}; vector<Ve> g; Graph(int n=0) : g(n) {} void EdgeD(int b, int e, E d = E()) {g[b].PB(Ed(d,e));} int topo; void TopoDfs(int v){ if (!g[v].t) { g[v].t=1; FOREACH(it,g[v]) TopoDfs(it->v); g[v].t=--topo; } } void TopoSort(){ FOREACH(it,g) it->t=0; topo=SIZE(g); FORD(x,topo-1,0) TopoDfs(x); } VI TopoSortV(){ VI res(SIZE(g)); TopoSort(); REP(x,SIZE(g)) res[g[x].t] = x; return res; } void Bfs(int s) { FOREACH(it, g) it->t=it->s=-1; g[s].t=0; int qu[SIZE(g)],b,e; qu[b=e=0]=s; while(b<=e) { s=qu[b++]; FOREACH(it, g[s]) if (g[it->v].t==-1) { g[qu[++e]=it->v].t=g[s].t+1; g[it->v].s=s; } } } void mvFlow(int v, Ed &e){ int u=e.v; g[u].PB(e); g[u].back().v=v; swap(g[v].back(),e); g[v].pop_back(); } int Ue; bool UFDfs(int v){ if (v==Ue) return true; g[v].s=1; FOREACH(it,g[v]) if (g[it->v].t==1+g[v].t && !g[it->v].s && UFDfs(it->v)){ mvFlow(v,*it); return true; } return false; } int UnitFlow(int v1,int v2){ int res=0; Ue=v2; while (1){ Bfs(v1); if (g[v2].t==-1) break; FOREACH(it,g) it->s=0; FOREACH(it,g[v1]) if (UFDfs(it->v)) {res++; mvFlow(v1, *it--);} } return res; } VI Hopcroft(){ int n=SIZE(g); VI res(n,-1); vector<char> l; if (!BiPart(l)) return res; g.resize(n+2); REP(i,n) if (!l[i]) EdgeD(n,i); else EdgeD(i,n+1); UnitFlow(n,n+1); REP(i,n) if (l[i] && g[i][0].v!=n+1) res[ res[g[i][0].v]=i ]=g[i][0].v; return res; } bool BiPart(vector<char> &v) { v.resize(SIZE(g),2); VI r = TopoSortV(); FOREACH(x, r) { if(v[*x]==2) v[*x]=0; FOREACH(it, g[*x]) if (v[it->v]==2) v[it->v] = 1-v[*x]; else if (v[it->v] == v[*x]) return 0; } return 1; } }; struct Ve {}; struct Vs {int t, s;}; int tab_rows[2002][2002]; void first_query(int n) { Graph<Vs, Ve> g(n * n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (tab_rows[i][j]) { for (int k = 0; k < n; k++) { if (!tab_rows[i][k]) { g.EdgeD(n * i + j, n * i + k); } if (!tab_rows[k][j]) { g.EdgeD(n * i + j, n * k + j); } } } } } VI res = g.Hopcroft(); int ret = 0; REP(x, SIZE(res)) if (res[x] > x) { ret++; } printf("%d\n", ret); } int main() { int n, m, q; scanf("%d %d %d", &n, &m, &q); for (int i = 0; i < m; i++) { int row1, row2, col1, col2; scanf("%d %d %d %d", &row1, &col1, &row2, &col2); --row1; --row2; --col1; --col2; for (int j = row1; j <= row2; j++) { tab_rows[j][col1]++; tab_rows[j][col2 + 1]--; } } for (int i = 0; i < n; i++) { int pref = 0; int s = 0; for (int j = 0; j < n; j++) { pref += tab_rows[i][j]; tab_rows[i][j] = pref % 2; } } first_query(n); for (int z = 0; z < q; z++) { int query_row, query_col; scanf("%d %d", &query_row, &query_col); --query_row; --query_col; if (tab_rows[query_row][query_col]) { tab_rows[query_row][query_col] = 0; } else { tab_rows[query_row][query_col] = 1; } Graph<Vs, Ve> g(n * n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (tab_rows[i][j]) { for (int k = 0; k < n; k++) { if (!tab_rows[i][k]) { g.EdgeD(n * i + j, n * i + k); } if (!tab_rows[k][j]) { g.EdgeD(n * i + j, n * k + j); } } } } } VI res = g.Hopcroft(); int ret =0; REP(x, SIZE(res)) if (res[x] > x) { ret++; } printf("%d\n", ret); } return 0; } |