6bf49a0393 2012-12-20 kinaba: #include <iostream> 6bf49a0393 2012-12-20 kinaba: #include <sstream> 6bf49a0393 2012-12-20 kinaba: #include <iomanip> 6bf49a0393 2012-12-20 kinaba: #include <vector> 6bf49a0393 2012-12-20 kinaba: #include <string> 6bf49a0393 2012-12-20 kinaba: #include <map> 6bf49a0393 2012-12-20 kinaba: #include <set> 6bf49a0393 2012-12-20 kinaba: #include <algorithm> 6bf49a0393 2012-12-20 kinaba: #include <numeric> 6bf49a0393 2012-12-20 kinaba: #include <iterator> 6bf49a0393 2012-12-20 kinaba: #include <functional> 6bf49a0393 2012-12-20 kinaba: #include <complex> 6bf49a0393 2012-12-20 kinaba: #include <queue> 6bf49a0393 2012-12-20 kinaba: #include <stack> 6bf49a0393 2012-12-20 kinaba: #include <cmath> 6bf49a0393 2012-12-20 kinaba: #include <cassert> 6bf49a0393 2012-12-20 kinaba: using namespace std; 6bf49a0393 2012-12-20 kinaba: typedef long long LL; 6bf49a0393 2012-12-20 kinaba: typedef long double LD; 6bf49a0393 2012-12-20 kinaba: typedef complex<LD> CMP; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: struct UnionFind 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: vector<int> uf, sz; 6bf49a0393 2012-12-20 kinaba: int nc; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) 6bf49a0393 2012-12-20 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } 6bf49a0393 2012-12-20 kinaba: int size() 6bf49a0393 2012-12-20 kinaba: { return nc; } 6bf49a0393 2012-12-20 kinaba: int size(int a) 6bf49a0393 2012-12-20 kinaba: { return sz[Find(a)]; } 6bf49a0393 2012-12-20 kinaba: int Find(int a) 6bf49a0393 2012-12-20 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } 6bf49a0393 2012-12-20 kinaba: bool Union(int a, int b) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: a = Find(a); 6bf49a0393 2012-12-20 kinaba: b = Find(b); 6bf49a0393 2012-12-20 kinaba: if( a != b ) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: if( sz[a] >= sz[b] ) swap(a, b); 6bf49a0393 2012-12-20 kinaba: uf[a] = b; 6bf49a0393 2012-12-20 kinaba: sz[b] += sz[a]; 6bf49a0393 2012-12-20 kinaba: --nc; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: return (a!=b); 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: }; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: class KnightCircuit2 { public: 6bf49a0393 2012-12-20 kinaba: int maxSize(int w, int h) 6bf49a0393 2012-12-20 kinaba: { 6bf49a0393 2012-12-20 kinaba: if( w>=4 && h>=4 ) 6bf49a0393 2012-12-20 kinaba: return w*h; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: UnionFind uf(w*h); 6bf49a0393 2012-12-20 kinaba: int dy[] = {+2,+2,-2,-2,+1,+1,-1,-1}; 6bf49a0393 2012-12-20 kinaba: int dx[] = {+1,-1,+1,-1,+2,-2,+2,-2}; 6bf49a0393 2012-12-20 kinaba: for(int y=0; y<h; ++y) 6bf49a0393 2012-12-20 kinaba: for(int x=0; x<w; ++x) 6bf49a0393 2012-12-20 kinaba: for(int d=0; d<8; ++d) { 6bf49a0393 2012-12-20 kinaba: int yy = y+dy[d]; 6bf49a0393 2012-12-20 kinaba: int xx = x+dx[d]; 6bf49a0393 2012-12-20 kinaba: if(0<=yy && yy<h && 0<=xx && xx<w) 6bf49a0393 2012-12-20 kinaba: uf.Union(y*w+x, yy*w+xx); 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: int m = 0; 6bf49a0393 2012-12-20 kinaba: for(int i=0; i<w*h; ++i) 6bf49a0393 2012-12-20 kinaba: m = max(m, uf.size(i)); 6bf49a0393 2012-12-20 kinaba: return m; 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: }; 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: // BEGIN CUT HERE 6bf49a0393 2012-12-20 kinaba: #include <ctime> 6bf49a0393 2012-12-20 kinaba: double start_time; string timer() 6bf49a0393 2012-12-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6bf49a0393 2012-12-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6bf49a0393 2012-12-20 kinaba: { os << "{ "; 6bf49a0393 2012-12-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6bf49a0393 2012-12-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6bf49a0393 2012-12-20 kinaba: void verify_case(const int& Expected, const int& Received) { 6bf49a0393 2012-12-20 kinaba: bool ok = (Expected == Received); 6bf49a0393 2012-12-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6bf49a0393 2012-12-20 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6bf49a0393 2012-12-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6bf49a0393 2012-12-20 kinaba: #define END verify_case(_, KnightCircuit2().maxSize(w, h));} 6bf49a0393 2012-12-20 kinaba: int main(){ 6bf49a0393 2012-12-20 kinaba: 6bf49a0393 2012-12-20 kinaba: CASE(0) 6bf49a0393 2012-12-20 kinaba: int w = 1; 6bf49a0393 2012-12-20 kinaba: int h = 1; 6bf49a0393 2012-12-20 kinaba: int _ = 1; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(1) 6bf49a0393 2012-12-20 kinaba: int w = 15; 6bf49a0393 2012-12-20 kinaba: int h = 2; 6bf49a0393 2012-12-20 kinaba: int _ = 8; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(2) 6bf49a0393 2012-12-20 kinaba: int w = 100; 6bf49a0393 2012-12-20 kinaba: int h = 100; 6bf49a0393 2012-12-20 kinaba: int _ = 10000; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: /* 6bf49a0393 2012-12-20 kinaba: CASE(3) 6bf49a0393 2012-12-20 kinaba: int w = ; 6bf49a0393 2012-12-20 kinaba: int h = ; 6bf49a0393 2012-12-20 kinaba: int _ = ; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: CASE(4) 6bf49a0393 2012-12-20 kinaba: int w = ; 6bf49a0393 2012-12-20 kinaba: int h = ; 6bf49a0393 2012-12-20 kinaba: int _ = ; 6bf49a0393 2012-12-20 kinaba: END 6bf49a0393 2012-12-20 kinaba: */ 6bf49a0393 2012-12-20 kinaba: } 6bf49a0393 2012-12-20 kinaba: // END CUT HERE