Artifact Content
Not logged in

Artifact 79806b5fc6258641594f0f062c635058509f370d


#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;

typedef int vert;
typedef vector< vector<vert> > graph;

class HatRack { public:
	long long countWays(vector <int> knob1, vector <int> knob2)
	{
		int N = knob1.size() + 1;
		graph G(N);
		for(int i=0; i<knob1.size(); ++i) {
			int u = knob1[i] - 1;
			int v = knob2[i] - 1;
			G[u].push_back(v);
			G[v].push_back(u);
		}
		memo.assign((N+1)*N*N, -1);
		LL total = 0;
		for(int root=0; root<N; ++root)
			total += solve(G, N, root);
		return total;
	}

	LL solve(const graph& G, int N, vert root)
	{
		return rec(G, N, N, root, 1);
	}

	vector<LL> memo;
	LL rec(const graph& G, const int N, int pre, int cur, int loc) {
		int key = (pre*N+cur)*N+(loc-1);
		if(memo[key] != -1)
			return memo[key];

		vector<int> e_child;
		if(loc*2+0 <= N) e_child.push_back(loc*2+0);
		if(loc*2+1 <= N) e_child.push_back(loc*2+1);

		vector<int> r_child;
		for(int i=0; i<G[cur].size(); ++i)
			if(G[cur][i] != pre)
				r_child.push_back(G[cur][i]);

		if(e_child.size() != r_child.size())
			return memo[key] = 0;

		LL total = 0;
		do {
			LL me = 1;
			for(int i=0; i<e_child.size(); ++i)
				me *= rec(G, N, cur, r_child[i], e_child[i]);
			total += me;
		} while( next_permutation(e_child.begin(), e_child.end()) );
		return memo[key] = total;
	}
};

// 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 long long& Expected, const long long& 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(_, HatRack().countWays(knob1, knob2));}
int main(){

CASE(0)
	int knob1_[] = {1};
	  vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 
	int knob2_[] = {2};
	  vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 
	long long _ = 2LL; 
END
CASE(1)
	int knob1_[] = {1,1};
	  vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 
	int knob2_[] = {2,3};
	  vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 
	long long _ = 2LL; 
END
CASE(2)
	int knob1_[] = {1,1,1,1};
	  vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 
	int knob2_[] = {2,3,4,5};
	  vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 
	long long _ = 0LL; 
END
CASE(3)
	int knob1_[] = {6,6,6,4,1};
	  vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 
	int knob2_[] = {1,2,4,5,3};
	  vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 
	long long _ = 0LL; 
END
CASE(4)
	int knob1_[] = {1,1,2,2,11,11,8,8,3,3,4,4,5};
	  vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 
	int knob2_[] = {2,3,11,8,12,13,9,10,4,5,7,6,14};
	  vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 
	long long _ = 16LL; 
END
CASE(5)
	int knob1_[] = {1,2,3,4,5,6,7,8,9};
	  vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 
	int knob2_[] = {2,3,4,5,6,7,8,9,10};
	  vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 
	long long _ = 0LL; 
END
/*
CASE(6)
	int knob1_[] = ;
	  vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 
	int knob2_[] = ;
	  vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 
	long long _ = LL; 
END
CASE(7)
	int knob1_[] = ;
	  vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 
	int knob2_[] = ;
	  vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 
	long long _ = LL; 
END
*/
}
// END CUT HERE