Artifact Content
Not logged in

Artifact 94cf308d5baaaf21d7db3b896fbe0716677174af


#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>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const LL MODVAL = 1234567891;
struct UnionFind
{
	vector<int> uf, sz, cyc;
	int nc;

	bool operator<(const UnionFind& rhs) const {
		if( nc != rhs.nc ) return nc < rhs.nc;
		if( uf != rhs.uf ) return uf < rhs.uf;
		if( sz != rhs.sz ) return sz < rhs.sz;
		return cyc < rhs.cyc;
	}

	UnionFind(int N): uf(N), sz(N,1), cyc(N), nc(N)
		{ for(int i=0; i<N; ++i) uf[i] = i; }
	int size()
		{ return nc; }
	int Find(int a)
		{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
	int 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];
				cyc[b] += cyc[a];
				--nc;
			}
			else
				cyc[a] ++;
			return cyc[a];
		}
};

class TheCitiesAndRoadsDivOne { public:
	int find(vector <string> map) 
	{
		int N = map.size();
		UnionFind uf(N);

		for(int i=0; i<N; ++i)
			for(int j=i+1; j<N; ++j)
				if( map[i][j]=='Y' )
					if( uf.Union(i, j) >= 2 )
						return 0;
		return rec(uf, 0, 1, N, map);
	}

	map< pair<UnionFind,int>, int > memo;
	int rec(UnionFind& uf, int i, int j , int N, vector<string>& map)
	{
		if( i == N )
			return uf.nc==1 ? 1 : 0;
		if( j == N )
			return rec(uf, i+1, i+2, N, map);

		pair<UnionFind,int> key(uf, i*N+j);
		if( memo.count(key) )
			return memo[key];

		LL cnt = rec(uf, i, j+1, N, map);
		if( map[i][j] == 'N' )
		{
			UnionFind uf2(uf);
			if( uf2.Union(i,j) < 2 )
				cnt = (cnt + rec(uf2,i,j+1,N,map)) % MODVAL;
		}
		return memo[key] = (int)cnt;
	}
};

// 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(_, TheCitiesAndRoadsDivOne().find(map));}
int main(){

CASE(0)
	string map_[] = {"NN",
 "NN"};
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = 1; 
END
CASE(1)
	string map_[] = {"NNY",
 "NNN",
 "YNN"};
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = 3; 
END
CASE(2)
	string map_[] = {"NY",
 "YN"};
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = 1; 
END
CASE(3)
	string map_[] = {"NYNN",
 "YNNN",
 "NNNY",
 "NNYN"};
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = 10; 
END
CASE(4)
	string map_[] = {"NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN",
 "NNNNNNNNNNNN"};
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = 1137797187; 
END
CASE(5)
	string map_[] = {"N"};
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = 1; 
END
CASE(6)
	string map_[] = {"NYYNN",
 "YNYNN",
 "YYNNN",
 "NNNNY",
 "NNNYN"};
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = 6; 
END
/*
CASE(7)
	string map_[] = ;
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = ; 
END
CASE(8)
	string map_[] = ;
	  vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); 
	int _ = ; 
END
*/
}
// END CUT HERE