1475bf9d41 2012-10-31 kinaba: #include <iostream> 1475bf9d41 2012-10-31 kinaba: #include <sstream> 1475bf9d41 2012-10-31 kinaba: #include <iomanip> 1475bf9d41 2012-10-31 kinaba: #include <vector> 1475bf9d41 2012-10-31 kinaba: #include <string> 1475bf9d41 2012-10-31 kinaba: #include <map> 1475bf9d41 2012-10-31 kinaba: #include <set> 1475bf9d41 2012-10-31 kinaba: #include <algorithm> 1475bf9d41 2012-10-31 kinaba: #include <numeric> 1475bf9d41 2012-10-31 kinaba: #include <iterator> 1475bf9d41 2012-10-31 kinaba: #include <functional> 1475bf9d41 2012-10-31 kinaba: #include <complex> 1475bf9d41 2012-10-31 kinaba: #include <queue> 1475bf9d41 2012-10-31 kinaba: #include <stack> 1475bf9d41 2012-10-31 kinaba: #include <cmath> 1475bf9d41 2012-10-31 kinaba: #include <cassert> 1475bf9d41 2012-10-31 kinaba: using namespace std; 1475bf9d41 2012-10-31 kinaba: typedef long long LL; 1475bf9d41 2012-10-31 kinaba: typedef long double LD; 1475bf9d41 2012-10-31 kinaba: typedef complex<LD> CMP; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: typedef int vert; 1475bf9d41 2012-10-31 kinaba: typedef vector< vector<vert> > graph; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: class HatRack { public: 1475bf9d41 2012-10-31 kinaba: long long countWays(vector <int> knob1, vector <int> knob2) 1475bf9d41 2012-10-31 kinaba: { 1475bf9d41 2012-10-31 kinaba: int N = knob1.size() + 1; 1475bf9d41 2012-10-31 kinaba: graph G(N); 1475bf9d41 2012-10-31 kinaba: for(int i=0; i<knob1.size(); ++i) { 1475bf9d41 2012-10-31 kinaba: int u = knob1[i] - 1; 1475bf9d41 2012-10-31 kinaba: int v = knob2[i] - 1; 1475bf9d41 2012-10-31 kinaba: G[u].push_back(v); 1475bf9d41 2012-10-31 kinaba: G[v].push_back(u); 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: memo.assign((N+1)*N*N, -1); 1475bf9d41 2012-10-31 kinaba: LL total = 0; 1475bf9d41 2012-10-31 kinaba: for(int root=0; root<N; ++root) 1475bf9d41 2012-10-31 kinaba: total += solve(G, N, root); 1475bf9d41 2012-10-31 kinaba: return total; 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: LL solve(const graph& G, int N, vert root) 1475bf9d41 2012-10-31 kinaba: { 1475bf9d41 2012-10-31 kinaba: return rec(G, N, N, root, 1); 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: vector<LL> memo; 1475bf9d41 2012-10-31 kinaba: LL rec(const graph& G, const int N, int pre, int cur, int loc) { 1475bf9d41 2012-10-31 kinaba: int key = (pre*N+cur)*N+(loc-1); 1475bf9d41 2012-10-31 kinaba: if(memo[key] != -1) 1475bf9d41 2012-10-31 kinaba: return memo[key]; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: vector<int> e_child; 1475bf9d41 2012-10-31 kinaba: if(loc*2+0 <= N) e_child.push_back(loc*2+0); 1475bf9d41 2012-10-31 kinaba: if(loc*2+1 <= N) e_child.push_back(loc*2+1); 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: vector<int> r_child; 1475bf9d41 2012-10-31 kinaba: for(int i=0; i<G[cur].size(); ++i) 1475bf9d41 2012-10-31 kinaba: if(G[cur][i] != pre) 1475bf9d41 2012-10-31 kinaba: r_child.push_back(G[cur][i]); 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: if(e_child.size() != r_child.size()) 1475bf9d41 2012-10-31 kinaba: return memo[key] = 0; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: LL total = 0; 1475bf9d41 2012-10-31 kinaba: do { 1475bf9d41 2012-10-31 kinaba: LL me = 1; 1475bf9d41 2012-10-31 kinaba: for(int i=0; i<e_child.size(); ++i) 1475bf9d41 2012-10-31 kinaba: me *= rec(G, N, cur, r_child[i], e_child[i]); 1475bf9d41 2012-10-31 kinaba: total += me; 1475bf9d41 2012-10-31 kinaba: } while( next_permutation(e_child.begin(), e_child.end()) ); 1475bf9d41 2012-10-31 kinaba: return memo[key] = total; 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: }; 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: // BEGIN CUT HERE 1475bf9d41 2012-10-31 kinaba: #include <ctime> 1475bf9d41 2012-10-31 kinaba: double start_time; string timer() 1475bf9d41 2012-10-31 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 1475bf9d41 2012-10-31 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 1475bf9d41 2012-10-31 kinaba: { os << "{ "; 1475bf9d41 2012-10-31 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 1475bf9d41 2012-10-31 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 1475bf9d41 2012-10-31 kinaba: void verify_case(const long long& Expected, const long long& Received) { 1475bf9d41 2012-10-31 kinaba: bool ok = (Expected == Received); 1475bf9d41 2012-10-31 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 1475bf9d41 2012-10-31 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 1475bf9d41 2012-10-31 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 1475bf9d41 2012-10-31 kinaba: #define END verify_case(_, HatRack().countWays(knob1, knob2));} 1475bf9d41 2012-10-31 kinaba: int main(){ 1475bf9d41 2012-10-31 kinaba: 1475bf9d41 2012-10-31 kinaba: CASE(0) 1475bf9d41 2012-10-31 kinaba: int knob1_[] = {1}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 1475bf9d41 2012-10-31 kinaba: int knob2_[] = {2}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 1475bf9d41 2012-10-31 kinaba: long long _ = 2LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(1) 1475bf9d41 2012-10-31 kinaba: int knob1_[] = {1,1}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 1475bf9d41 2012-10-31 kinaba: int knob2_[] = {2,3}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 1475bf9d41 2012-10-31 kinaba: long long _ = 2LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(2) 1475bf9d41 2012-10-31 kinaba: int knob1_[] = {1,1,1,1}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 1475bf9d41 2012-10-31 kinaba: int knob2_[] = {2,3,4,5}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 1475bf9d41 2012-10-31 kinaba: long long _ = 0LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(3) 1475bf9d41 2012-10-31 kinaba: int knob1_[] = {6,6,6,4,1}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 1475bf9d41 2012-10-31 kinaba: int knob2_[] = {1,2,4,5,3}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 1475bf9d41 2012-10-31 kinaba: long long _ = 0LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(4) 1475bf9d41 2012-10-31 kinaba: int knob1_[] = {1,1,2,2,11,11,8,8,3,3,4,4,5}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 1475bf9d41 2012-10-31 kinaba: int knob2_[] = {2,3,11,8,12,13,9,10,4,5,7,6,14}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 1475bf9d41 2012-10-31 kinaba: long long _ = 16LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(5) 1475bf9d41 2012-10-31 kinaba: int knob1_[] = {1,2,3,4,5,6,7,8,9}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 1475bf9d41 2012-10-31 kinaba: int knob2_[] = {2,3,4,5,6,7,8,9,10}; 1475bf9d41 2012-10-31 kinaba: vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 1475bf9d41 2012-10-31 kinaba: long long _ = 0LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: /* 1475bf9d41 2012-10-31 kinaba: CASE(6) 1475bf9d41 2012-10-31 kinaba: int knob1_[] = ; 1475bf9d41 2012-10-31 kinaba: vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 1475bf9d41 2012-10-31 kinaba: int knob2_[] = ; 1475bf9d41 2012-10-31 kinaba: vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 1475bf9d41 2012-10-31 kinaba: long long _ = LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: CASE(7) 1475bf9d41 2012-10-31 kinaba: int knob1_[] = ; 1475bf9d41 2012-10-31 kinaba: vector <int> knob1(knob1_, knob1_+sizeof(knob1_)/sizeof(*knob1_)); 1475bf9d41 2012-10-31 kinaba: int knob2_[] = ; 1475bf9d41 2012-10-31 kinaba: vector <int> knob2(knob2_, knob2_+sizeof(knob2_)/sizeof(*knob2_)); 1475bf9d41 2012-10-31 kinaba: long long _ = LL; 1475bf9d41 2012-10-31 kinaba: END 1475bf9d41 2012-10-31 kinaba: */ 1475bf9d41 2012-10-31 kinaba: } 1475bf9d41 2012-10-31 kinaba: // END CUT HERE