062d1ffb7c 2016-03-28 kinaba: #include <iostream> 062d1ffb7c 2016-03-28 kinaba: #include <sstream> 062d1ffb7c 2016-03-28 kinaba: #include <iomanip> 062d1ffb7c 2016-03-28 kinaba: #include <vector> 062d1ffb7c 2016-03-28 kinaba: #include <string> 062d1ffb7c 2016-03-28 kinaba: #include <map> 062d1ffb7c 2016-03-28 kinaba: #include <set> 062d1ffb7c 2016-03-28 kinaba: #include <algorithm> 062d1ffb7c 2016-03-28 kinaba: #include <numeric> 062d1ffb7c 2016-03-28 kinaba: #include <iterator> 062d1ffb7c 2016-03-28 kinaba: #include <functional> 062d1ffb7c 2016-03-28 kinaba: #include <complex> 062d1ffb7c 2016-03-28 kinaba: #include <queue> 062d1ffb7c 2016-03-28 kinaba: #include <stack> 062d1ffb7c 2016-03-28 kinaba: #include <cmath> 062d1ffb7c 2016-03-28 kinaba: #include <cassert> 062d1ffb7c 2016-03-28 kinaba: #include <tuple> 062d1ffb7c 2016-03-28 kinaba: #include <cstring> 062d1ffb7c 2016-03-28 kinaba: using namespace std; 062d1ffb7c 2016-03-28 kinaba: typedef long long LL; 062d1ffb7c 2016-03-28 kinaba: typedef complex<double> CMP; 062d1ffb7c 2016-03-28 kinaba: 062d1ffb7c 2016-03-28 kinaba: LL nei[2000][32]; 062d1ffb7c 2016-03-28 kinaba: 062d1ffb7c 2016-03-28 kinaba: class SmilesTheFriendshipUnicorn { public: 062d1ffb7c 2016-03-28 kinaba: string hasFriendshipChain(int N, vector <int> A, vector <int> B) 062d1ffb7c 2016-03-28 kinaba: { 062d1ffb7c 2016-03-28 kinaba: return solve(N, A, B) ? "Yay!" : ":("; 062d1ffb7c 2016-03-28 kinaba: } 062d1ffb7c 2016-03-28 kinaba: 062d1ffb7c 2016-03-28 kinaba: bool solve(int N, const vector<int>& A, const vector<int>& B) 062d1ffb7c 2016-03-28 kinaba: { 062d1ffb7c 2016-03-28 kinaba: vector<vector<int>> G(N); 062d1ffb7c 2016-03-28 kinaba: for(int i=0; i<A.size(); ++i) { 062d1ffb7c 2016-03-28 kinaba: G[A[i]].push_back(B[i]); 062d1ffb7c 2016-03-28 kinaba: G[B[i]].push_back(A[i]); 062d1ffb7c 2016-03-28 kinaba: } 062d1ffb7c 2016-03-28 kinaba: 062d1ffb7c 2016-03-28 kinaba: memset(&nei, 0, sizeof(nei)); 062d1ffb7c 2016-03-28 kinaba: 062d1ffb7c 2016-03-28 kinaba: for(int i=0; i<A.size(); ++i) { 062d1ffb7c 2016-03-28 kinaba: for(int u: G[A[i]]) 062d1ffb7c 2016-03-28 kinaba: nei[i][u>>6] |= 1LL<<(u&0x3f); 062d1ffb7c 2016-03-28 kinaba: for(int u: G[B[i]]) 062d1ffb7c 2016-03-28 kinaba: nei[i][u>>6] |= 1LL<<(u&0x3f); 062d1ffb7c 2016-03-28 kinaba: nei[i][A[i]>>6] &= ~(1LL<<(A[i]&0x3f)); 062d1ffb7c 2016-03-28 kinaba: nei[i][B[i]>>6] &= ~(1LL<<(B[i]&0x3f)); 062d1ffb7c 2016-03-28 kinaba: } 062d1ffb7c 2016-03-28 kinaba: 062d1ffb7c 2016-03-28 kinaba: for(int i=0; i<A.size(); ++i) 062d1ffb7c 2016-03-28 kinaba: for(int k=i+1; k<A.size(); ++k) 062d1ffb7c 2016-03-28 kinaba: if(A[i]!=A[k] && A[i]!=B[k] && B[i]!=A[k] && B[i]!=B[k]) 062d1ffb7c 2016-03-28 kinaba: for(int v=0; v<32; ++v) 062d1ffb7c 2016-03-28 kinaba: if(nei[i][v] & nei[k][v]) 062d1ffb7c 2016-03-28 kinaba: return true; 062d1ffb7c 2016-03-28 kinaba: return false; 062d1ffb7c 2016-03-28 kinaba: } 062d1ffb7c 2016-03-28 kinaba: }; 062d1ffb7c 2016-03-28 kinaba: 062d1ffb7c 2016-03-28 kinaba: // BEGIN CUT HERE 062d1ffb7c 2016-03-28 kinaba: #include <ctime> 062d1ffb7c 2016-03-28 kinaba: double start_time; string timer() 062d1ffb7c 2016-03-28 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 062d1ffb7c 2016-03-28 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 062d1ffb7c 2016-03-28 kinaba: { os << "{ "; 062d1ffb7c 2016-03-28 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 062d1ffb7c 2016-03-28 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 062d1ffb7c 2016-03-28 kinaba: void verify_case(const string& Expected, const string& Received) { 062d1ffb7c 2016-03-28 kinaba: bool ok = (Expected == Received); 062d1ffb7c 2016-03-28 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 062d1ffb7c 2016-03-28 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 062d1ffb7c 2016-03-28 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 062d1ffb7c 2016-03-28 kinaba: #define END verify_case(_, SmilesTheFriendshipUnicorn().hasFriendshipChain(N, A, B));} 062d1ffb7c 2016-03-28 kinaba: int main(){ 062d1ffb7c 2016-03-28 kinaba: 062d1ffb7c 2016-03-28 kinaba: CASE(0) 062d1ffb7c 2016-03-28 kinaba: int N = 5; 062d1ffb7c 2016-03-28 kinaba: int A_[] = {0, 1, 2, 3}; 062d1ffb7c 2016-03-28 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 062d1ffb7c 2016-03-28 kinaba: int B_[] = {1, 2, 3, 4}; 062d1ffb7c 2016-03-28 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 062d1ffb7c 2016-03-28 kinaba: string _ = "Yay!"; 062d1ffb7c 2016-03-28 kinaba: END 062d1ffb7c 2016-03-28 kinaba: CASE(1) 062d1ffb7c 2016-03-28 kinaba: int N = 5; 062d1ffb7c 2016-03-28 kinaba: int A_[] = {0, 1, 2, 3, 1}; 062d1ffb7c 2016-03-28 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 062d1ffb7c 2016-03-28 kinaba: int B_[] = {1, 2, 3, 0, 4}; 062d1ffb7c 2016-03-28 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 062d1ffb7c 2016-03-28 kinaba: string _ = "Yay!"; 062d1ffb7c 2016-03-28 kinaba: END 062d1ffb7c 2016-03-28 kinaba: CASE(2) 062d1ffb7c 2016-03-28 kinaba: int N = 6; 062d1ffb7c 2016-03-28 kinaba: int A_[] = {0, 0, 0, 0, 0}; 062d1ffb7c 2016-03-28 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 062d1ffb7c 2016-03-28 kinaba: int B_[] = {1, 2, 3, 4, 5}; 062d1ffb7c 2016-03-28 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 062d1ffb7c 2016-03-28 kinaba: string _ = ":("; 062d1ffb7c 2016-03-28 kinaba: END 062d1ffb7c 2016-03-28 kinaba: CASE(3) 062d1ffb7c 2016-03-28 kinaba: int N = 8; 062d1ffb7c 2016-03-28 kinaba: int A_[] = {1, 3, 4, 3, 4, 3, 0, 2}; 062d1ffb7c 2016-03-28 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 062d1ffb7c 2016-03-28 kinaba: int B_[] = {7, 7, 7, 4, 6, 5, 4, 7}; 062d1ffb7c 2016-03-28 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 062d1ffb7c 2016-03-28 kinaba: string _ = "Yay!"; 062d1ffb7c 2016-03-28 kinaba: END 062d1ffb7c 2016-03-28 kinaba: CASE(4) 062d1ffb7c 2016-03-28 kinaba: int N = 7; 062d1ffb7c 2016-03-28 kinaba: int A_[] = {0, 1, 1, 5, 4, 5}; 062d1ffb7c 2016-03-28 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 062d1ffb7c 2016-03-28 kinaba: int B_[] = {5, 2, 3, 6, 1, 1}; 062d1ffb7c 2016-03-28 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 062d1ffb7c 2016-03-28 kinaba: string _ = ":("; 062d1ffb7c 2016-03-28 kinaba: END 062d1ffb7c 2016-03-28 kinaba: CASE(5) 062d1ffb7c 2016-03-28 kinaba: int N = 42; 062d1ffb7c 2016-03-28 kinaba: int A_[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41} 062d1ffb7c 2016-03-28 kinaba: ; 062d1ffb7c 2016-03-28 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 062d1ffb7c 2016-03-28 kinaba: int B_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 0}; 062d1ffb7c 2016-03-28 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 062d1ffb7c 2016-03-28 kinaba: string _ = "Yay!"; 062d1ffb7c 2016-03-28 kinaba: END 062d1ffb7c 2016-03-28 kinaba: /* 062d1ffb7c 2016-03-28 kinaba: CASE(6) 062d1ffb7c 2016-03-28 kinaba: int N = ; 062d1ffb7c 2016-03-28 kinaba: int A_[] = ; 062d1ffb7c 2016-03-28 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 062d1ffb7c 2016-03-28 kinaba: int B_[] = ; 062d1ffb7c 2016-03-28 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 062d1ffb7c 2016-03-28 kinaba: string _ = ; 062d1ffb7c 2016-03-28 kinaba: END 062d1ffb7c 2016-03-28 kinaba: CASE(7) 062d1ffb7c 2016-03-28 kinaba: int N = ; 062d1ffb7c 2016-03-28 kinaba: int A_[] = ; 062d1ffb7c 2016-03-28 kinaba: vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 062d1ffb7c 2016-03-28 kinaba: int B_[] = ; 062d1ffb7c 2016-03-28 kinaba: vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 062d1ffb7c 2016-03-28 kinaba: string _ = ; 062d1ffb7c 2016-03-28 kinaba: END 062d1ffb7c 2016-03-28 kinaba: */ 062d1ffb7c 2016-03-28 kinaba: } 062d1ffb7c 2016-03-28 kinaba: // END CUT HERE