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
| #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<vector> #include<queue> #include<stack> #include<bitset> #include<set> using namespace std;
typedef long long ll; #define MAXN 1006 #define MAXM 450 #define pb push_back #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define inf 0x3f3f3f3f #define cmx( a , b ) a = max( a , b ) #define cmn( a , b ) a = min( a , b ) #define upd( a , b ) ( a = ( a + b ) % P ) #define P 998244353 void read( signed& x ) { scanf("%d",&x); } void read( ll& x ) { scanf("%lld",&x); } int n , m , s , t; #define maxm 5000006 #define maxn 500000
#define rep(ii, a, b) for (int ii = a; ii <= b; ++ii) #define per(ii, a, b) for (int ii = b; ii >= a; --ii) #define IO \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define show(x) cout << #x << "=" << x << endl #define show2(x, y) cout << #x << "=" << x << " " << #y << "=" << y << endl #define show3(x, y, z) cout << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl #define show4(w, x, y, z) \ cout << #w << "=" << w << " " << #x << "=" << x << " " << #y << "=" << y << " " << #z << "=" << z << endl #define show5(v, w, x, y, z) \ cout << #v << "=" << v << " " << #w << "=" << w << " " << #x << "=" << x << " " << #y << "=" << y << " " \ << #z << "=" << z << endl #define showa(x, a, b) \ cout << #x << ": "; \ rep(i, a, b) cout << x[i] << ' '; \ cout << endl
template <typename T> class mxf { public: struct node { int to, next; T cap; } e[maxm]; int cur[maxn], head[maxn], dis[maxn], gap[maxn]; int nume = 1, s, t, tot; void init(int n) { rep(i, 0, n) head[i] = gap[i] = dis[i] = 0; nume = 1; } void add(int a, int b, T c) { e[++nume] = { b, head[a], c }; head[a] = nume; e[++nume] = { a, head[b], 0 }; head[b] = nume; } T dfs(int now, T flow) { if (now == t || flow <= 0) return flow; T use = 0, tmp; int d = dis[now] - 1, to; for (int &i = cur[now]; i; i = e[i].next) { if (dis[to = e[i].to] == d && (tmp = e[i].cap)) { e[i].cap -= (tmp = dfs(to, min(flow - use, tmp))); e[i ^ 1].cap += tmp; if ((use += tmp) == flow) return use; } } if (!--gap[dis[now]]) dis[s] = tot + 1; ++gap[++dis[now]]; cur[now] = head[now]; return use; } ll getflow(int ss, int tt, int n) { tot = n; s = ss; t = tt; gap[0] = tot; ll ans = 0; memcpy(cur, head, (tot + 1) << 2); while (dis[s] <= tot) ans += dfs(s, 0x7fffffff); return ans; } }; mxf<int> net;
int G[MAXN][MAXN] , id[MAXN][MAXN] , cn; int main() {
read( n ) , read( m ); for( int i = 1 , s ; i <= n ; ++ i ) for( int j = 1 ; j <= m ; ++ j ) { scanf("%d",&s); if( s == 2 ) id[i][j] = ++ cn; else if( s == 1 ) G[i][j] = 1; } for( int i = 1 ; i <= n ; ++ i ) for( int j = 1 ; j <= m ; ++ j ) if( G[i][j] ) { ++ cn; net.add( cn , cn + 1 , 1 ); if( i & 1 ) { if( id[i - 1][j] ) { if( id[i][j - 1] ) net.add( id[i - 1][j] , cn , 1 ) , net.add( cn + 1 , id[i][j - 1] , 1 ); if( id[i][j + 1] ) net.add( id[i - 1][j] , cn , 1 ) , net.add( cn + 1 , id[i][j + 1] , 1 ); } if( id[i + 1][j] ) { if( id[i][j - 1] ) net.add( id[i + 1][j] , cn , 1 ) , net.add( cn + 1 , id[i][j - 1] , 1 ); if( id[i][j + 1] ) net.add( id[i + 1][j] , cn , 1 ) , net.add( cn + 1 , id[i][j + 1] , 1 ); } } else { if( id[i][j - 1] ) { if( id[i - 1][j] ) net.add( id[i][j - 1] , cn , 1 ) , net.add( cn + 1 , id[i - 1][j] , 1 ); if( id[i + 1][j] ) net.add( id[i][j - 1] , cn , 1 ) , net.add( cn + 1 , id[i + 1][j] , 1 ); } if( id[i][j + 1] ) { if( id[i - 1][j] ) net.add( id[i][j + 1] , cn , 1 ) , net.add( cn + 1 , id[i - 1][j] , 1 ); if( id[i + 1][j] ) net.add( id[i][j + 1] , cn , 1 ) , net.add( cn + 1 , id[i + 1][j] , 1 ); } } ++ cn; } s = ++ cn , t = ++ cn; for( int i = 1 ; i <= n ; ++ i ) for( int j = 1 ; j <= m ; ++ j ) if( id[i][j] ) if( ( ~i ) & 1 ) net.add( s , id[i][j] , 1 ); else net.add( id[i][j] , t , 1 ); cout << net.getflow( s , t , t ) << endl; }
|