743781a2d9 2012-06-26 kinaba: #include <iostream> 743781a2d9 2012-06-26 kinaba: #include <sstream> 743781a2d9 2012-06-26 kinaba: #include <iomanip> 743781a2d9 2012-06-26 kinaba: #include <vector> 743781a2d9 2012-06-26 kinaba: #include <string> 743781a2d9 2012-06-26 kinaba: #include <map> 743781a2d9 2012-06-26 kinaba: #include <set> 743781a2d9 2012-06-26 kinaba: #include <algorithm> 743781a2d9 2012-06-26 kinaba: #include <numeric> 743781a2d9 2012-06-26 kinaba: #include <iterator> 743781a2d9 2012-06-26 kinaba: #include <functional> 743781a2d9 2012-06-26 kinaba: #include <complex> 743781a2d9 2012-06-26 kinaba: #include <queue> 743781a2d9 2012-06-26 kinaba: #include <stack> 743781a2d9 2012-06-26 kinaba: #include <cmath> 743781a2d9 2012-06-26 kinaba: #include <cassert> 743781a2d9 2012-06-26 kinaba: using namespace std; 743781a2d9 2012-06-26 kinaba: typedef long long LL; 743781a2d9 2012-06-26 kinaba: typedef long double LD; 743781a2d9 2012-06-26 kinaba: typedef complex<LD> CMP; 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: //------------------------------------------------------------- 743781a2d9 2012-06-26 kinaba: // Dinic's Algorithm 743781a2d9 2012-06-26 kinaba: // O(V E) 743781a2d9 2012-06-26 kinaba: // 743781a2d9 2012-06-26 kinaba: // G : bidirectional (G[i].has(j) <==> G[j].has(i)) 743781a2d9 2012-06-26 kinaba: // F : flow-capacity F[i][j] = Capacity, F[j][i] = 0 743781a2d9 2012-06-26 kinaba: // 743781a2d9 2012-06-26 kinaba: // Verified by 743781a2d9 2012-06-26 kinaba: // - SRM 399 Div1 LV3 743781a2d9 2012-06-26 kinaba: // - PKU 1459 743781a2d9 2012-06-26 kinaba: // - CodeCraft 09 CUTS 743781a2d9 2012-06-26 kinaba: // - SRM 465 Div1 LV2 743781a2d9 2012-06-26 kinaba: // - SRM 543 Div1 LV3 743781a2d9 2012-06-26 kinaba: //------------------------------------------------------------- 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: template<typename T> 743781a2d9 2012-06-26 kinaba: class IdGen 743781a2d9 2012-06-26 kinaba: { 743781a2d9 2012-06-26 kinaba: map<T, int> v2id_; 743781a2d9 2012-06-26 kinaba: vector<T> id2v_; 743781a2d9 2012-06-26 kinaba: public: 743781a2d9 2012-06-26 kinaba: int v2id(const T& v) { 743781a2d9 2012-06-26 kinaba: if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); } 743781a2d9 2012-06-26 kinaba: return v2id_[v]; 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: const T& id2v(int i) const { return id2v_[i]; } 743781a2d9 2012-06-26 kinaba: int size() const { return id2v_.size(); } 743781a2d9 2012-06-26 kinaba: }; 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: template<typename Vert, typename Flow, int NV=2048> 743781a2d9 2012-06-26 kinaba: class MaxFlow 743781a2d9 2012-06-26 kinaba: { 743781a2d9 2012-06-26 kinaba: IdGen<Vert> idgen; 743781a2d9 2012-06-26 kinaba: vector<int> G[NV]; 743781a2d9 2012-06-26 kinaba: Flow F[NV][NV]; 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: public: 743781a2d9 2012-06-26 kinaba: void addEdge( Vert s_, Vert t_, Flow f ) 743781a2d9 2012-06-26 kinaba: { 743781a2d9 2012-06-26 kinaba: const int s = idgen.v2id(s_), t = idgen.v2id(t_); 743781a2d9 2012-06-26 kinaba: G[s].push_back(t); 743781a2d9 2012-06-26 kinaba: G[t].push_back(s); 743781a2d9 2012-06-26 kinaba: F[s][t] = f; 743781a2d9 2012-06-26 kinaba: F[t][s] = 0; 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: Flow calc( Vert s_, Vert t_ ) 743781a2d9 2012-06-26 kinaba: { 743781a2d9 2012-06-26 kinaba: const int S = idgen.v2id(s_), D = idgen.v2id(t_); 743781a2d9 2012-06-26 kinaba: for( Flow total=0 ;; ) { 743781a2d9 2012-06-26 kinaba: // Do BFS and compute the level for each node. 743781a2d9 2012-06-26 kinaba: int LV[NV] = {0}; 743781a2d9 2012-06-26 kinaba: vector<int> Q(1, S); 743781a2d9 2012-06-26 kinaba: for(int lv=1; !Q.empty(); ++lv) { 743781a2d9 2012-06-26 kinaba: vector<int> Q2; 743781a2d9 2012-06-26 kinaba: for(size_t i=0; i!=Q.size(); ++i) { 743781a2d9 2012-06-26 kinaba: const vector<int>& ne = G[Q[i]]; 743781a2d9 2012-06-26 kinaba: for(size_t j=0; j!=ne.size(); ++j) 743781a2d9 2012-06-26 kinaba: if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S ) 743781a2d9 2012-06-26 kinaba: LV[ne[j]]=lv, Q2.push_back(ne[j]); 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: Q.swap(Q2); 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: // Destination is now unreachable. Done. 743781a2d9 2012-06-26 kinaba: if( !LV[D] ) 743781a2d9 2012-06-26 kinaba: return total; 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: // Iterating DFS. 743781a2d9 2012-06-26 kinaba: bool blocked[NV] = {}; 743781a2d9 2012-06-26 kinaba: total += dinic_dfs( S, D, LV, 0x7fffffff, blocked ); 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: private: 743781a2d9 2012-06-26 kinaba: Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] ) 743781a2d9 2012-06-26 kinaba: { 743781a2d9 2012-06-26 kinaba: Flow flow_out = 0; 743781a2d9 2012-06-26 kinaba: for(size_t i=0; i!=G[v].size(); ++i) { 743781a2d9 2012-06-26 kinaba: int u = G[v][i]; 743781a2d9 2012-06-26 kinaba: if( LV[v]+1==LV[u] && F[v][u] ) { 743781a2d9 2012-06-26 kinaba: Flow f = min(flow_in-flow_out, F[v][u]); 743781a2d9 2012-06-26 kinaba: if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) { 743781a2d9 2012-06-26 kinaba: F[v][u] -= f; 743781a2d9 2012-06-26 kinaba: F[u][v] += f; 743781a2d9 2012-06-26 kinaba: flow_out += f; 743781a2d9 2012-06-26 kinaba: if( flow_in == flow_out ) return flow_out; 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: blocked[v] = (flow_out==0); 743781a2d9 2012-06-26 kinaba: return flow_out; 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: }; 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: class FoxAndCake { public: 743781a2d9 2012-06-26 kinaba: string ableToDivide(int W, int H, vector <int> x, vector <int> y) 743781a2d9 2012-06-26 kinaba: { 743781a2d9 2012-06-26 kinaba: vector< vector<char> > M = compress(W, H, x, y); 743781a2d9 2012-06-26 kinaba: H = M.size(); 743781a2d9 2012-06-26 kinaba: W = M[0].size(); 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: typedef pair< int, pair<int,int> > Vert; 743781a2d9 2012-06-26 kinaba: enum {IN, OUT, SPECIAL}; 743781a2d9 2012-06-26 kinaba: Vert Src(SPECIAL, make_pair(0, 0)); 743781a2d9 2012-06-26 kinaba: Vert Sink(SPECIAL, make_pair(1, 1)); 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: MaxFlow<Vert,int,7*7*7*2+2> G; 743781a2d9 2012-06-26 kinaba: for(int y=0; y<H; ++y) 743781a2d9 2012-06-26 kinaba: for(int x=0; x<W; ++x) if( M[y][x]!='#' ) 743781a2d9 2012-06-26 kinaba: { 743781a2d9 2012-06-26 kinaba: int dy[] = {-1,+1,0,0}; 743781a2d9 2012-06-26 kinaba: int dx[] = {0,0,-1,+1}; 743781a2d9 2012-06-26 kinaba: for(int d=0; d<4; ++d) { 743781a2d9 2012-06-26 kinaba: int yy = y + dy[d]; 743781a2d9 2012-06-26 kinaba: int xx = x + dx[d]; 743781a2d9 2012-06-26 kinaba: if(0<=yy&&yy<H&&0<=xx&&xx<W&&M[yy][xx]!='#') 743781a2d9 2012-06-26 kinaba: G.addEdge(Vert(OUT, make_pair(y,x)), Vert(IN, make_pair(yy,xx)), 1); 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: G.addEdge(Vert(IN, make_pair(y,x)), Vert(OUT, make_pair(y,x)), 1); 743781a2d9 2012-06-26 kinaba: if(M[y][x]=='C') G.addEdge(Src, Vert(IN, make_pair(y,x)), 1); 743781a2d9 2012-06-26 kinaba: if(M[y][x]=='S') G.addEdge(Vert(OUT, make_pair(y,x)), Sink, 1); 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: return G.calc(Src, Sink)==3 ? "Yes" : "No"; 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: vector< vector<char> > compress(int W, int H, vector<int>& x, vector<int>& y) 743781a2d9 2012-06-26 kinaba: { 743781a2d9 2012-06-26 kinaba: for(int i=0; i<7; ++i) 743781a2d9 2012-06-26 kinaba: --x[i], --y[i]; 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: set<int> sigx, sigy; 743781a2d9 2012-06-26 kinaba: for(int i=0; i<7; ++i) 743781a2d9 2012-06-26 kinaba: for(int d=-3; d<=+3; ++d) { 743781a2d9 2012-06-26 kinaba: if(0<=x[i]+d && x[i]+d<W) 743781a2d9 2012-06-26 kinaba: sigx.insert(x[i]+d); 743781a2d9 2012-06-26 kinaba: if(0<=y[i]+d && y[i]+d<H) 743781a2d9 2012-06-26 kinaba: sigy.insert(y[i]+d); 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: vector<int> sx(sigx.begin(), sigx.end()); 743781a2d9 2012-06-26 kinaba: vector<int> sy(sigy.begin(), sigy.end()); 743781a2d9 2012-06-26 kinaba: map<int,int> inv_sx; for(int i=0; i<sx.size(); ++i) inv_sx[sx[i]] = i; 743781a2d9 2012-06-26 kinaba: map<int,int> inv_sy; for(int i=0; i<sy.size(); ++i) inv_sy[sy[i]] = i; 743781a2d9 2012-06-26 kinaba: W = sx.size(); 743781a2d9 2012-06-26 kinaba: H = sy.size(); 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: vector< vector<char> > result(H, vector<char>(W, ' ')); 743781a2d9 2012-06-26 kinaba: for(int i=0; i<7; ++i) { 743781a2d9 2012-06-26 kinaba: int xx = inv_sx[x[i]]; 743781a2d9 2012-06-26 kinaba: int yy = inv_sy[y[i]]; 743781a2d9 2012-06-26 kinaba: result[yy][xx] = (i==0?'#':i<4?'C':'S'); 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: return result; 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: }; 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: // BEGIN CUT HERE 743781a2d9 2012-06-26 kinaba: #include <ctime> 743781a2d9 2012-06-26 kinaba: double start_time; string timer() 743781a2d9 2012-06-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 743781a2d9 2012-06-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 743781a2d9 2012-06-26 kinaba: { os << "{ "; 743781a2d9 2012-06-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 743781a2d9 2012-06-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 743781a2d9 2012-06-26 kinaba: void verify_case(const string& Expected, const string& Received) { 743781a2d9 2012-06-26 kinaba: bool ok = (Expected == Received); 743781a2d9 2012-06-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 743781a2d9 2012-06-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 743781a2d9 2012-06-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 743781a2d9 2012-06-26 kinaba: #define END verify_case(_, FoxAndCake().ableToDivide(n, m, x, y));} 743781a2d9 2012-06-26 kinaba: int main(){ 743781a2d9 2012-06-26 kinaba: 743781a2d9 2012-06-26 kinaba: CASE(0) 743781a2d9 2012-06-26 kinaba: int n = 2; 743781a2d9 2012-06-26 kinaba: int m = 4; 743781a2d9 2012-06-26 kinaba: int x_[] = {1,1,1,1,2,2,2}; 743781a2d9 2012-06-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 743781a2d9 2012-06-26 kinaba: int y_[] = {1,2,3,4,2,3,4}; 743781a2d9 2012-06-26 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 743781a2d9 2012-06-26 kinaba: string _ = "Yes"; 743781a2d9 2012-06-26 kinaba: END 743781a2d9 2012-06-26 kinaba: CASE(1) 743781a2d9 2012-06-26 kinaba: int n = 2; 743781a2d9 2012-06-26 kinaba: int m = 4; 743781a2d9 2012-06-26 kinaba: int x_[] = {1,1,2,1,2,1,2}; 743781a2d9 2012-06-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 743781a2d9 2012-06-26 kinaba: int y_[] = {1,2,2,3,3,4,4}; 743781a2d9 2012-06-26 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 743781a2d9 2012-06-26 kinaba: string _ = "No"; 743781a2d9 2012-06-26 kinaba: END 743781a2d9 2012-06-26 kinaba: CASE(2) 743781a2d9 2012-06-26 kinaba: int n = 6; 743781a2d9 2012-06-26 kinaba: int m = 6; 743781a2d9 2012-06-26 kinaba: int x_[] = {1,1,3,4,3,4,5}; 743781a2d9 2012-06-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 743781a2d9 2012-06-26 kinaba: int y_[] = {2,6,4,5,5,4,2}; 743781a2d9 2012-06-26 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 743781a2d9 2012-06-26 kinaba: string _ = "Yes"; 743781a2d9 2012-06-26 kinaba: END 743781a2d9 2012-06-26 kinaba: CASE(3) 743781a2d9 2012-06-26 kinaba: int n = 999999999; 743781a2d9 2012-06-26 kinaba: int m = 999999999; 743781a2d9 2012-06-26 kinaba: int x_[] = {500000000,1,1,1,999999999,999999999,999999999}; 743781a2d9 2012-06-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 743781a2d9 2012-06-26 kinaba: int y_[] = {500000000,1,2,3,999999997,999999998,999999999}; 743781a2d9 2012-06-26 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 743781a2d9 2012-06-26 kinaba: string _ = "Yes"; 743781a2d9 2012-06-26 kinaba: END 743781a2d9 2012-06-26 kinaba: CASE(4) 743781a2d9 2012-06-26 kinaba: int n = 1000000000; 743781a2d9 2012-06-26 kinaba: int m = 1000000000; 743781a2d9 2012-06-26 kinaba: int x_[] = {500000000,1,1,2,999999998,999999999,999999999}; 743781a2d9 2012-06-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 743781a2d9 2012-06-26 kinaba: int y_[] = {500000000,1,2,1,999999999,999999998,999999999}; 743781a2d9 2012-06-26 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 743781a2d9 2012-06-26 kinaba: string _ = "No"; 743781a2d9 2012-06-26 kinaba: END 743781a2d9 2012-06-26 kinaba: CASE(5) 743781a2d9 2012-06-26 kinaba: int n = 7; 743781a2d9 2012-06-26 kinaba: int m = 2; 743781a2d9 2012-06-26 kinaba: int x_[] = {4,1,2,3,5,6,7}; 743781a2d9 2012-06-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 743781a2d9 2012-06-26 kinaba: int y_[] = {2,1,2,2,2,2,1}; 743781a2d9 2012-06-26 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 743781a2d9 2012-06-26 kinaba: string _ = "No"; 743781a2d9 2012-06-26 kinaba: END 743781a2d9 2012-06-26 kinaba: CASE(6) 743781a2d9 2012-06-26 kinaba: int n = 7; 743781a2d9 2012-06-26 kinaba: int m = 2; 743781a2d9 2012-06-26 kinaba: int x_[] = {5,1,2,4,3,6,7}; 743781a2d9 2012-06-26 kinaba: vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 743781a2d9 2012-06-26 kinaba: int y_[] = {1,1,1,1,1,1,1}; 743781a2d9 2012-06-26 kinaba: vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); 743781a2d9 2012-06-26 kinaba: string _ = "No"; 743781a2d9 2012-06-26 kinaba: END 743781a2d9 2012-06-26 kinaba: } 743781a2d9 2012-06-26 kinaba: // END CUT HERE