#include <bits/stdc++.h> using namespace std; struct e{ int a, flow, cap; }; vector<e> edges; vector<vector<int>> graf; vector<int> cost, nast; int s, t; void addEdge(int a, int b, int c){ e e1 = {b, 0, c}; graf[a].push_back(edges.size()); edges.push_back(e1); e e2 = {a, 0, 0}; graf[b].push_back(edges.size()); edges.push_back(e2); } void resetDinic(){ for(int i = 0; i < edges.size(); i++){ edges[i].flow = 0; } } bool bfs(){ queue<int> q; q.push(s); cost.resize(0); cost.resize(t+1, 1e9); cost[s] = 0; while(q.size()){ int cur = q.front(); q.pop(); for(auto i:graf[cur]){ e it = edges[i]; if((it.flow<it.cap)&&cost[it.a]==1e9){ q.push(it.a); cost[it.a] = cost[cur]+1; } } } return cost[t]!=1e9; } int dfs(int x, int limit){ if(limit==0) return -1; if(x==t){ return limit; } for(int i = nast[x]; nast[x]<graf[x].size(); nast[x]++){ i = nast[x]; int id = graf[x][i]; e cur = edges[id]; int a = cur.a; if(cost[x]+1==cost[a]){ int tmp = dfs(a, min(limit, cur.cap-cur.flow)); if(tmp!=-1){ edges[id].flow+=tmp; edges[id^1].flow-=tmp; return tmp; } } } return -1; } int dinic(){ resetDinic(); int out = 0; while(bfs()){ nast.resize(0); nast.resize(t+1, 0); int tmp = 0; while(tmp!=-1){ out+=tmp; tmp = dfs(s, 1e9); } } return out; } int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); int n, m, q; cin>>n>>m>>q; s=n*n+1; t=n*n+2; graf.resize(t+1); vector<vector<int>> pref(n, vector<int>(n)); vector<vector<int>> tab(n, vector<int>(n)); for(int i = 0; i < m; i++){ int a, b, c, d; cin>>a>>b>>c>>d; a--; b--; c--; d--; pref[a][b]++; if(c<n-1) pref[c+1][b]--; if(d<n-1) pref[a][d+1]--; if(c<n-1&&d<n-1) pref[c+1][d+1]++; } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ tab[i][j] = pref[i][j]; if(i>0) tab[i][j] += tab[i-1][j]; if(j>0) tab[i][j] += tab[i][j-1]; if(i>0&&j>0) tab[i][j] -= tab[i-1][j-1]; } } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ int cur = i*n+j; if(tab[i][j]%2){ addEdge(s, cur, 1); for(int k = 0; k < n; k++){ if(k!=i) if(tab[k][j]%2==0) addEdge(cur, k*n+j, 1); if(k!=j) if(tab[i][k]%2==0) addEdge(cur, i*n+k, 1); } }else{ addEdge(cur, t, 1); } } } cout<<dinic()<<'\n'; for(int i = 0; i < q; i++){ int a, b; cin>>a>>b; a--; b--; tab[a][b]++; graf.resize(0); edges.resize(0); graf.resize(t+1); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ int cur = i*n+j; if(tab[i][j]%2){ addEdge(s, cur, 1); for(int k = 0; k < n; k++){ if(k!=i) if(tab[k][j]%2==0) addEdge(cur, k*n+j, 1); if(k!=j) if(tab[i][k]%2==0) addEdge(cur, i*n+k, 1); } }else{ addEdge(cur, t, 1); } } } cout<<dinic()<<'\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 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 | #include <bits/stdc++.h> using namespace std; struct e{ int a, flow, cap; }; vector<e> edges; vector<vector<int>> graf; vector<int> cost, nast; int s, t; void addEdge(int a, int b, int c){ e e1 = {b, 0, c}; graf[a].push_back(edges.size()); edges.push_back(e1); e e2 = {a, 0, 0}; graf[b].push_back(edges.size()); edges.push_back(e2); } void resetDinic(){ for(int i = 0; i < edges.size(); i++){ edges[i].flow = 0; } } bool bfs(){ queue<int> q; q.push(s); cost.resize(0); cost.resize(t+1, 1e9); cost[s] = 0; while(q.size()){ int cur = q.front(); q.pop(); for(auto i:graf[cur]){ e it = edges[i]; if((it.flow<it.cap)&&cost[it.a]==1e9){ q.push(it.a); cost[it.a] = cost[cur]+1; } } } return cost[t]!=1e9; } int dfs(int x, int limit){ if(limit==0) return -1; if(x==t){ return limit; } for(int i = nast[x]; nast[x]<graf[x].size(); nast[x]++){ i = nast[x]; int id = graf[x][i]; e cur = edges[id]; int a = cur.a; if(cost[x]+1==cost[a]){ int tmp = dfs(a, min(limit, cur.cap-cur.flow)); if(tmp!=-1){ edges[id].flow+=tmp; edges[id^1].flow-=tmp; return tmp; } } } return -1; } int dinic(){ resetDinic(); int out = 0; while(bfs()){ nast.resize(0); nast.resize(t+1, 0); int tmp = 0; while(tmp!=-1){ out+=tmp; tmp = dfs(s, 1e9); } } return out; } int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); int n, m, q; cin>>n>>m>>q; s=n*n+1; t=n*n+2; graf.resize(t+1); vector<vector<int>> pref(n, vector<int>(n)); vector<vector<int>> tab(n, vector<int>(n)); for(int i = 0; i < m; i++){ int a, b, c, d; cin>>a>>b>>c>>d; a--; b--; c--; d--; pref[a][b]++; if(c<n-1) pref[c+1][b]--; if(d<n-1) pref[a][d+1]--; if(c<n-1&&d<n-1) pref[c+1][d+1]++; } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ tab[i][j] = pref[i][j]; if(i>0) tab[i][j] += tab[i-1][j]; if(j>0) tab[i][j] += tab[i][j-1]; if(i>0&&j>0) tab[i][j] -= tab[i-1][j-1]; } } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ int cur = i*n+j; if(tab[i][j]%2){ addEdge(s, cur, 1); for(int k = 0; k < n; k++){ if(k!=i) if(tab[k][j]%2==0) addEdge(cur, k*n+j, 1); if(k!=j) if(tab[i][k]%2==0) addEdge(cur, i*n+k, 1); } }else{ addEdge(cur, t, 1); } } } cout<<dinic()<<'\n'; for(int i = 0; i < q; i++){ int a, b; cin>>a>>b; a--; b--; tab[a][b]++; graf.resize(0); edges.resize(0); graf.resize(t+1); for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ int cur = i*n+j; if(tab[i][j]%2){ addEdge(s, cur, 1); for(int k = 0; k < n; k++){ if(k!=i) if(tab[k][j]%2==0) addEdge(cur, k*n+j, 1); if(k!=j) if(tab[i][k]%2==0) addEdge(cur, i*n+k, 1); } }else{ addEdge(cur, t, 1); } } } cout<<dinic()<<'\n'; } return 0; } |