Artifact Content
Not logged in

Artifact 15bba962603caf183c76b83d3a1da8e4a3550889


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N): uf(N), sz(N,1), nc(N)
		{ for(int i=0; i<N; ++i) uf[i] = i; }
	int size()
		{ return nc; }
	int size(int a)
		{ return sz[Find(a)]; }
	int Find(int a)
		{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
	bool Union(int a, int b)
		{
			a = Find(a);
			b = Find(b);
			if( a != b )
			{
				if( sz[a] >= sz[b] ) swap(a, b);
				uf[a] = b;
				sz[b] += sz[a];
				--nc;
			}
			return (a!=b);
		}
};

class KnightCircuit2 { public:
	int maxSize(int w, int h)
	{
		if( w>=4 && h>=4 )
			return w*h;

		UnionFind uf(w*h);
		int dy[] = {+2,+2,-2,-2,+1,+1,-1,-1};
		int dx[] = {+1,-1,+1,-1,+2,-2,+2,-2};
		for(int y=0; y<h; ++y)
		for(int x=0; x<w; ++x)
		for(int d=0; d<8; ++d) {
			int yy = y+dy[d];
			int xx = x+dx[d];
			if(0<=yy && yy<h && 0<=xx && xx<w)
				uf.Union(y*w+x, yy*w+xx);
		}

		int m = 0;
		for(int i=0; i<w*h; ++i)
			m = max(m, uf.size(i));
		return m;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, KnightCircuit2().maxSize(w, h));}
int main(){

CASE(0)
	int w = 1; 
	int h = 1; 
	int _ = 1; 
END
CASE(1)
	int w = 15; 
	int h = 2; 
	int _ = 8; 
END
CASE(2)
	int w = 100; 
	int h = 100; 
	int _ = 10000; 
END
/*
CASE(3)
	int w = ; 
	int h = ; 
	int _ = ; 
END
CASE(4)
	int w = ; 
	int h = ; 
	int _ = ; 
END
*/
}
// END CUT HERE