cda9cb7744 2013-08-27 kinaba: #include <iostream> cda9cb7744 2013-08-27 kinaba: #include <sstream> cda9cb7744 2013-08-27 kinaba: #include <iomanip> cda9cb7744 2013-08-27 kinaba: #include <vector> cda9cb7744 2013-08-27 kinaba: #include <string> cda9cb7744 2013-08-27 kinaba: #include <map> cda9cb7744 2013-08-27 kinaba: #include <set> cda9cb7744 2013-08-27 kinaba: #include <algorithm> cda9cb7744 2013-08-27 kinaba: #include <numeric> cda9cb7744 2013-08-27 kinaba: #include <iterator> cda9cb7744 2013-08-27 kinaba: #include <functional> cda9cb7744 2013-08-27 kinaba: #include <complex> cda9cb7744 2013-08-27 kinaba: #include <queue> cda9cb7744 2013-08-27 kinaba: #include <stack> cda9cb7744 2013-08-27 kinaba: #include <cmath> cda9cb7744 2013-08-27 kinaba: #include <cassert> cda9cb7744 2013-08-27 kinaba: #include <tuple> cda9cb7744 2013-08-27 kinaba: using namespace std; cda9cb7744 2013-08-27 kinaba: typedef long long LL; cda9cb7744 2013-08-27 kinaba: typedef complex<double> CMP; cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: class ThreeColorability { public: cda9cb7744 2013-08-27 kinaba: vector <string> lexSmallest(vector <string> cells) cda9cb7744 2013-08-27 kinaba: { cda9cb7744 2013-08-27 kinaba: if(!can(cells)) cda9cb7744 2013-08-27 kinaba: return vector<string>(); cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: for(int y=0; y<cells.size(); ++y) cda9cb7744 2013-08-27 kinaba: for(int x=0; x<cells[y].size(); ++x) cda9cb7744 2013-08-27 kinaba: if(cells[y][x] == '?') cda9cb7744 2013-08-27 kinaba: { cda9cb7744 2013-08-27 kinaba: cells[y][x] = 'N'; cda9cb7744 2013-08-27 kinaba: if(!can(cells)) cda9cb7744 2013-08-27 kinaba: cells[y][x] = 'Z'; cda9cb7744 2013-08-27 kinaba: } cda9cb7744 2013-08-27 kinaba: return cells; cda9cb7744 2013-08-27 kinaba: } cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: enum {R,G,B}; cda9cb7744 2013-08-27 kinaba: enum {RG,RB,GR,GB,BR,BG}; cda9cb7744 2013-08-27 kinaba: bool can(const vector<string>& C) cda9cb7744 2013-08-27 kinaba: { cda9cb7744 2013-08-27 kinaba: int N = C[0].size(); cda9cb7744 2013-08-27 kinaba: vector<int> P(N, (1<<6)-1); cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: for(int y=0; y<C.size(); ++y) { cda9cb7744 2013-08-27 kinaba: apply(P, C[y]); cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: int can = (1<<R)|(1<<G)|(1<<B); cda9cb7744 2013-08-27 kinaba: for(int x=0; x<N; ++x) cda9cb7744 2013-08-27 kinaba: { cda9cb7744 2013-08-27 kinaba: int can2 = 0; cda9cb7744 2013-08-27 kinaba: if((can&(1<<R))&&(P[x]&(1<<RG))) can2 |= 1<<G; cda9cb7744 2013-08-27 kinaba: if((can&(1<<R))&&(P[x]&(1<<RB))) can2 |= 1<<B; cda9cb7744 2013-08-27 kinaba: if((can&(1<<G))&&(P[x]&(1<<GR))) can2 |= 1<<R; cda9cb7744 2013-08-27 kinaba: if((can&(1<<G))&&(P[x]&(1<<GB))) can2 |= 1<<B; cda9cb7744 2013-08-27 kinaba: if((can&(1<<B))&&(P[x]&(1<<BR))) can2 |= 1<<R; cda9cb7744 2013-08-27 kinaba: if((can&(1<<B))&&(P[x]&(1<<BG))) can2 |= 1<<G; cda9cb7744 2013-08-27 kinaba: can = can2; cda9cb7744 2013-08-27 kinaba: } cda9cb7744 2013-08-27 kinaba: if(!can) cda9cb7744 2013-08-27 kinaba: return false; cda9cb7744 2013-08-27 kinaba: } cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: return true; cda9cb7744 2013-08-27 kinaba: } cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: void apply(vector<int>& P, const string& C) cda9cb7744 2013-08-27 kinaba: { cda9cb7744 2013-08-27 kinaba: for(int i=0; i<C.size(); ++i) cda9cb7744 2013-08-27 kinaba: apply(P[i], C[i]); cda9cb7744 2013-08-27 kinaba: } cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: void apply(int& P, char C) cda9cb7744 2013-08-27 kinaba: { cda9cb7744 2013-08-27 kinaba: int Q = 0; cda9cb7744 2013-08-27 kinaba: // N cda9cb7744 2013-08-27 kinaba: if((P&(1<<RG))&&(C!='Z')) Q |= (1<<GB); cda9cb7744 2013-08-27 kinaba: if((P&(1<<RB))&&(C!='Z')) Q |= (1<<BG); cda9cb7744 2013-08-27 kinaba: if((P&(1<<GR))&&(C!='Z')) Q |= (1<<RB); cda9cb7744 2013-08-27 kinaba: if((P&(1<<GB))&&(C!='Z')) Q |= (1<<BR); cda9cb7744 2013-08-27 kinaba: if((P&(1<<BR))&&(C!='Z')) Q |= (1<<RG); cda9cb7744 2013-08-27 kinaba: if((P&(1<<BG))&&(C!='Z')) Q |= (1<<GR); cda9cb7744 2013-08-27 kinaba: // Z cda9cb7744 2013-08-27 kinaba: if((P&(1<<RG))&&(C!='N')) Q |= (1<<BR); cda9cb7744 2013-08-27 kinaba: if((P&(1<<RB))&&(C!='N')) Q |= (1<<GR); cda9cb7744 2013-08-27 kinaba: if((P&(1<<GR))&&(C!='N')) Q |= (1<<BG); cda9cb7744 2013-08-27 kinaba: if((P&(1<<GB))&&(C!='N')) Q |= (1<<RG); cda9cb7744 2013-08-27 kinaba: if((P&(1<<BR))&&(C!='N')) Q |= (1<<GB); cda9cb7744 2013-08-27 kinaba: if((P&(1<<BG))&&(C!='N')) Q |= (1<<RB); cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: P = Q; cda9cb7744 2013-08-27 kinaba: } cda9cb7744 2013-08-27 kinaba: }; cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: // BEGIN CUT HERE cda9cb7744 2013-08-27 kinaba: #include <ctime> cda9cb7744 2013-08-27 kinaba: double start_time; string timer() cda9cb7744 2013-08-27 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } cda9cb7744 2013-08-27 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) cda9cb7744 2013-08-27 kinaba: { os << "{ "; cda9cb7744 2013-08-27 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) cda9cb7744 2013-08-27 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } cda9cb7744 2013-08-27 kinaba: void verify_case(const vector <string>& Expected, const vector <string>& Received) { cda9cb7744 2013-08-27 kinaba: bool ok = (Expected == Received); cda9cb7744 2013-08-27 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cda9cb7744 2013-08-27 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } cda9cb7744 2013-08-27 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); cda9cb7744 2013-08-27 kinaba: #define END verify_case(_, ThreeColorability().lexSmallest(cells));} cda9cb7744 2013-08-27 kinaba: int main(){ cda9cb7744 2013-08-27 kinaba: cda9cb7744 2013-08-27 kinaba: CASE(0) cda9cb7744 2013-08-27 kinaba: string cells_[] = {"Z"}; cda9cb7744 2013-08-27 kinaba: vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); cda9cb7744 2013-08-27 kinaba: string __[] = {"Z" }; cda9cb7744 2013-08-27 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); cda9cb7744 2013-08-27 kinaba: END cda9cb7744 2013-08-27 kinaba: CASE(1) cda9cb7744 2013-08-27 kinaba: string cells_[] = {"??", "?N"}; cda9cb7744 2013-08-27 kinaba: vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); cda9cb7744 2013-08-27 kinaba: string __[] = {"NN", "NN" }; cda9cb7744 2013-08-27 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); cda9cb7744 2013-08-27 kinaba: END cda9cb7744 2013-08-27 kinaba: CASE(2) cda9cb7744 2013-08-27 kinaba: string cells_[] = {"ZZZ", "ZNZ"}; cda9cb7744 2013-08-27 kinaba: vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); cda9cb7744 2013-08-27 kinaba: vector <string> _; cda9cb7744 2013-08-27 kinaba: END cda9cb7744 2013-08-27 kinaba: CASE(3) cda9cb7744 2013-08-27 kinaba: string cells_[] = {"N?N??NN","??ZN??Z","NN???Z?","ZZZ?Z??","Z???NN?","N?????N","ZZ?N?NN"}; cda9cb7744 2013-08-27 kinaba: vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); cda9cb7744 2013-08-27 kinaba: vector <string> _; cda9cb7744 2013-08-27 kinaba: END cda9cb7744 2013-08-27 kinaba: CASE(4) cda9cb7744 2013-08-27 kinaba: string cells_[] = {"ZZZZ","ZZZZ","ZZZZ"}; cda9cb7744 2013-08-27 kinaba: vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); cda9cb7744 2013-08-27 kinaba: string __[] = {"ZZZZ", "ZZZZ", "ZZZZ" }; cda9cb7744 2013-08-27 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); cda9cb7744 2013-08-27 kinaba: END cda9cb7744 2013-08-27 kinaba: /* cda9cb7744 2013-08-27 kinaba: CASE(5) cda9cb7744 2013-08-27 kinaba: string cells_[] = ; cda9cb7744 2013-08-27 kinaba: vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); cda9cb7744 2013-08-27 kinaba: string __[] = ; cda9cb7744 2013-08-27 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); cda9cb7744 2013-08-27 kinaba: END cda9cb7744 2013-08-27 kinaba: CASE(6) cda9cb7744 2013-08-27 kinaba: string cells_[] = ; cda9cb7744 2013-08-27 kinaba: vector <string> cells(cells_, cells_+sizeof(cells_)/sizeof(*cells_)); cda9cb7744 2013-08-27 kinaba: string __[] = ; cda9cb7744 2013-08-27 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); cda9cb7744 2013-08-27 kinaba: END cda9cb7744 2013-08-27 kinaba: */ cda9cb7744 2013-08-27 kinaba: } cda9cb7744 2013-08-27 kinaba: // END CUT HERE