4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class BunnyExam { public: 4fd800b3a8 2011-02-23 kinaba: double getExpected(int m, int k, vector <int> linkage) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // trivially impossible cases 4fd800b3a8 2011-02-23 kinaba: if( k==1 && m>1 ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: if( k==2 ) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<linkage.size(); i+=2) 4fd800b3a8 2011-02-23 kinaba: if( (linkage[i] ^ linkage[i+1])&1 ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<linkage.size(); i+=2) 4fd800b3a8 2011-02-23 kinaba: if( abs(linkage[i] - linkage[i+1]) == 1 ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // interference graph 4fd800b3a8 2011-02-23 kinaba: int M = linkage.size()/2; 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > conflict(M); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<M; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<M; ++j) 4fd800b3a8 2011-02-23 kinaba: if( abs(linkage[2*i+0]-linkage[2*j+0])==1 || abs(linkage[2*i+1]-linkage[2*j+0])==1 4fd800b3a8 2011-02-23 kinaba: || abs(linkage[2*i+0]-linkage[2*j+1])==1 || abs(linkage[2*i+1]-linkage[2*j+1])==1 ) 4fd800b3a8 2011-02-23 kinaba: conflict[i].push_back(j); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // check k-colorability 4fd800b3a8 2011-02-23 kinaba: if( colorable(conflict, k) ) 4fd800b3a8 2011-02-23 kinaba: return double(m)/k; 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool colorable(vector< vector<int> >& G, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( G.size() <= k ) 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: vector<int> color(G.size(), (1<<k)-1); 4fd800b3a8 2011-02-23 kinaba: color[0] = 1; 4fd800b3a8 2011-02-23 kinaba: return rec(G, color, 0); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool rec(vector< vector<int> >& G, const vector<int>& color, int i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( i == color.size() ) 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: for(int k=1; k<=color[i]; k<<=1) 4fd800b3a8 2011-02-23 kinaba: if( color[i] & k ) // use color k 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> c2 = color; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<G[i].size(); ++j) // G[i][j] cannot use k 4fd800b3a8 2011-02-23 kinaba: c2[G[i][j]] &= ~k; 4fd800b3a8 2011-02-23 kinaba: if( rec(G, c2, i+1) ) 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const double& Expected, const double& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, BunnyExam().getExpected(m, k, linkage));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int m = 3; 4fd800b3a8 2011-02-23 kinaba: int k = 2; 4fd800b3a8 2011-02-23 kinaba: int linkage_[] = { 1, 3 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 1.5; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int m = 4; 4fd800b3a8 2011-02-23 kinaba: int k = 2; 4fd800b3a8 2011-02-23 kinaba: int linkage_[] = { 1, 4 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 4fd800b3a8 2011-02-23 kinaba: double _ = -1.0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int m = 2; 4fd800b3a8 2011-02-23 kinaba: int k = 8; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage; 4fd800b3a8 2011-02-23 kinaba: double _ = 0.25; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int m = 1000000000; 4fd800b3a8 2011-02-23 kinaba: int k = 1; 4fd800b3a8 2011-02-23 kinaba: int linkage_[] = { 11, 13, 2010, 487 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 4fd800b3a8 2011-02-23 kinaba: double _ = -1.0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int m = 128; 4fd800b3a8 2011-02-23 kinaba: int k = 64; 4fd800b3a8 2011-02-23 kinaba: int linkage_[] = { 32, 16, 8, 4, 2, 1 } 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 4fd800b3a8 2011-02-23 kinaba: double _ = -1.0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int m = 13; 4fd800b3a8 2011-02-23 kinaba: int k = 3; 4fd800b3a8 2011-02-23 kinaba: int linkage_[] = { 1, 3, 7, 9, 13, 10, 6, 2 } 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 4.333333333333333; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int m = 8; 4fd800b3a8 2011-02-23 kinaba: int k = 3; 4fd800b3a8 2011-02-23 kinaba: int linkage_[] = {1,4,2,6,3,8,5,7}; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 4fd800b3a8 2011-02-23 kinaba: double _ = -1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(7) 4fd800b3a8 2011-02-23 kinaba: int m = 1000000000; 4fd800b3a8 2011-02-23 kinaba: int k = 4; 4fd800b3a8 2011-02-23 kinaba: int linkage_[] = {1,4,2,6,3,8,5,7,9,19, 10,20,11,21,12,22,13,23,14,24}; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 4fd800b3a8 2011-02-23 kinaba: double _ = -1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(8) 4fd800b3a8 2011-02-23 kinaba: int m = 20; 4fd800b3a8 2011-02-23 kinaba: int k = 3; 4fd800b3a8 2011-02-23 kinaba: int linkage_[] = {19, 6, 1, 14, 13, 9, 20, 10, 15, 8, 17, 5, 12, 7, 16, 2, 18, 4, 3, 11}; 4fd800b3a8 2011-02-23 kinaba: vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 6.666666666666667; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE