#include <bits/stdc++.h> using namespace std; const int MX=2002,MV=(MX+8)*MX,ME=4*MV,inf=1000000000; int S,T,n,m,Q,e,i,j,xa,ya,xb,yb,cur,res,a[MX][MX],w[MX][MX],t[ME],f[ME],c[ME],pre[MV],dst[MV],q[MV],ptr[MV]; vector<int> g[MV]; int dfs(int i, int cr) { if (i==T) return cr; for (; ptr[i]<g[i].size(); ++ptr[i]) { int ed=g[i][ptr[i]],k=t[ed]; if (dst[k]==dst[i]+1) { int fl=min(cr,c[ed]-f[ed]); if (fl>0) fl=dfs(k,fl); if (fl>0) { f[ed]+=fl; f[ed^1]-=fl; return fl; } } } return 0; } bool pe(int S, int T) { for (int i=0; i<=T; i++) dst[i]=inf; int fi=0,fr=1; q[0]=S; dst[S]=0; while (fi<fr) { int i=q[fi++]; if (i==T) break; for (int j=int(g[i].size())-1; j>=0; --j) if (f[g[i][j]]<c[g[i][j]]) { int y=t[g[i][j]]; if (dst[y]>dst[i]+1) { dst[y]=dst[i]+1; q[fr++]=y; } } } return dst[T]<inf; } bool peo(int S, int T) { for (int i=0; i<=T; i++) dst[i]=inf; int fi=0,fr=1; q[0]=S; dst[S]=0; while (fi<fr) { int i=q[fi++]; if (i==T) break; for (int j=0; j<g[i].size(); j++) if (f[g[i][j]]<0) { int y=t[g[i][j]]; if (dst[y]>dst[i]+1) { dst[y]=dst[i]+1; pre[y]=g[i][j]; q[fr++]=y; } } } return dst[T]<inf; } bool pef(int S, int T) { for (int i=0; i<=T; i++) dst[i]=inf; int fi=0,fr=1; q[0]=S; dst[S]=0; while (fi<fr) { int i=q[fi++]; if (i==T) break; for (int j=0; j<g[i].size(); j++) if (f[g[i][j]]>0) { int y=t[g[i][j]]; if (dst[y]>dst[i]+1) { dst[y]=dst[i]+1; pre[y]=g[i][j]; q[fr++]=y; } } } return dst[T]<inf; } int edg(int fr, int to, int v, int o) { t[e]=to; f[e]=0; c[e]=v; g[fr].push_back(e++); t[e]=fr; f[e]=0; c[e]=o; g[to].push_back(e++); return e-2; } void runflow() { while (pe(S,T)) { for (i=0; i<=T; i++) ptr[i]=0; int x=0; for (; x=dfs(S,inf); res+=x); } printf("%d\n",res); } int main() { scanf("%d%d%d",&n,&m,&Q); while (m--) { scanf("%d%d%d%d",&xa,&ya,&xb,&yb); a[xa][ya]^=1; a[xa][yb+1]^=1; a[xb+1][ya]^=1; a[xb+1][yb+1]^=1; } S=n*n+n+n; T=S+1; for (i=1; i<=n; i++) for (j=1; j<=n; j++) { a[i][j]^=a[i-1][j]; a[i][j]^=a[i][j-1]; a[i][j]^=a[i-1][j-1]; cur=(i-1)*n+j-1; edg(cur,n*n+i-1,1,1); edg(cur,n*n+n+j-1,1,1); w[i][j]=(a[i][j]?edg(cur,S,0,1):edg(cur,T,1,0)); } runflow(); while (Q--) { scanf("%d%d",&xa,&ya); xb=w[xa][ya]; a[xa][ya]^=1; cur=(xa-1)*n+ya-1; if (f[xb]) { if (a[xa][ya]) { peo(cur,S); for (i=S; i!=cur; i=t[pre[i]^1]) { ++f[pre[i]]; --f[pre[i]^1]; } } else { pef(cur,T); for (i=T; i!=cur; i=t[pre[i]^1]) { --f[pre[i]]; ++f[pre[i]^1]; } } f[xb]=f[xb^1]=0; --res; } c[xb]=c[xb^1]=0; w[xa][ya]=(a[xa][ya]?edg(cur,S,0,1):edg(cur,T,1,0)); runflow(); } 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 | #include <bits/stdc++.h> using namespace std; const int MX=2002,MV=(MX+8)*MX,ME=4*MV,inf=1000000000; int S,T,n,m,Q,e,i,j,xa,ya,xb,yb,cur,res,a[MX][MX],w[MX][MX],t[ME],f[ME],c[ME],pre[MV],dst[MV],q[MV],ptr[MV]; vector<int> g[MV]; int dfs(int i, int cr) { if (i==T) return cr; for (; ptr[i]<g[i].size(); ++ptr[i]) { int ed=g[i][ptr[i]],k=t[ed]; if (dst[k]==dst[i]+1) { int fl=min(cr,c[ed]-f[ed]); if (fl>0) fl=dfs(k,fl); if (fl>0) { f[ed]+=fl; f[ed^1]-=fl; return fl; } } } return 0; } bool pe(int S, int T) { for (int i=0; i<=T; i++) dst[i]=inf; int fi=0,fr=1; q[0]=S; dst[S]=0; while (fi<fr) { int i=q[fi++]; if (i==T) break; for (int j=int(g[i].size())-1; j>=0; --j) if (f[g[i][j]]<c[g[i][j]]) { int y=t[g[i][j]]; if (dst[y]>dst[i]+1) { dst[y]=dst[i]+1; q[fr++]=y; } } } return dst[T]<inf; } bool peo(int S, int T) { for (int i=0; i<=T; i++) dst[i]=inf; int fi=0,fr=1; q[0]=S; dst[S]=0; while (fi<fr) { int i=q[fi++]; if (i==T) break; for (int j=0; j<g[i].size(); j++) if (f[g[i][j]]<0) { int y=t[g[i][j]]; if (dst[y]>dst[i]+1) { dst[y]=dst[i]+1; pre[y]=g[i][j]; q[fr++]=y; } } } return dst[T]<inf; } bool pef(int S, int T) { for (int i=0; i<=T; i++) dst[i]=inf; int fi=0,fr=1; q[0]=S; dst[S]=0; while (fi<fr) { int i=q[fi++]; if (i==T) break; for (int j=0; j<g[i].size(); j++) if (f[g[i][j]]>0) { int y=t[g[i][j]]; if (dst[y]>dst[i]+1) { dst[y]=dst[i]+1; pre[y]=g[i][j]; q[fr++]=y; } } } return dst[T]<inf; } int edg(int fr, int to, int v, int o) { t[e]=to; f[e]=0; c[e]=v; g[fr].push_back(e++); t[e]=fr; f[e]=0; c[e]=o; g[to].push_back(e++); return e-2; } void runflow() { while (pe(S,T)) { for (i=0; i<=T; i++) ptr[i]=0; int x=0; for (; x=dfs(S,inf); res+=x); } printf("%d\n",res); } int main() { scanf("%d%d%d",&n,&m,&Q); while (m--) { scanf("%d%d%d%d",&xa,&ya,&xb,&yb); a[xa][ya]^=1; a[xa][yb+1]^=1; a[xb+1][ya]^=1; a[xb+1][yb+1]^=1; } S=n*n+n+n; T=S+1; for (i=1; i<=n; i++) for (j=1; j<=n; j++) { a[i][j]^=a[i-1][j]; a[i][j]^=a[i][j-1]; a[i][j]^=a[i-1][j-1]; cur=(i-1)*n+j-1; edg(cur,n*n+i-1,1,1); edg(cur,n*n+n+j-1,1,1); w[i][j]=(a[i][j]?edg(cur,S,0,1):edg(cur,T,1,0)); } runflow(); while (Q--) { scanf("%d%d",&xa,&ya); xb=w[xa][ya]; a[xa][ya]^=1; cur=(xa-1)*n+ya-1; if (f[xb]) { if (a[xa][ya]) { peo(cur,S); for (i=S; i!=cur; i=t[pre[i]^1]) { ++f[pre[i]]; --f[pre[i]^1]; } } else { pef(cur,T); for (i=T; i!=cur; i=t[pre[i]^1]) { --f[pre[i]]; ++f[pre[i]^1]; } } f[xb]=f[xb^1]=0; --res; } c[xb]=c[xb^1]=0; w[xa][ya]=(a[xa][ya]?edg(cur,S,0,1):edg(cur,T,1,0)); runflow(); } return 0; } |