8166493378 2012-05-12 kinaba: #include <iostream> 8166493378 2012-05-12 kinaba: #include <sstream> 8166493378 2012-05-12 kinaba: #include <iomanip> 8166493378 2012-05-12 kinaba: #include <vector> 8166493378 2012-05-12 kinaba: #include <string> 8166493378 2012-05-12 kinaba: #include <map> 8166493378 2012-05-12 kinaba: #include <set> 8166493378 2012-05-12 kinaba: #include <algorithm> 8166493378 2012-05-12 kinaba: #include <numeric> 8166493378 2012-05-12 kinaba: #include <iterator> 8166493378 2012-05-12 kinaba: #include <functional> 8166493378 2012-05-12 kinaba: #include <complex> 8166493378 2012-05-12 kinaba: #include <queue> 8166493378 2012-05-12 kinaba: #include <stack> 8166493378 2012-05-12 kinaba: #include <cmath> 8166493378 2012-05-12 kinaba: #include <cassert> 8166493378 2012-05-12 kinaba: #include <cstring> 8166493378 2012-05-12 kinaba: #ifdef __GNUC__ 8166493378 2012-05-12 kinaba: #include <ext/hash_map> 8166493378 2012-05-12 kinaba: #define unordered_map __gnu_cxx::hash_map 8166493378 2012-05-12 kinaba: #else 8166493378 2012-05-12 kinaba: #include <unordered_map> 8166493378 2012-05-12 kinaba: #endif 8166493378 2012-05-12 kinaba: using namespace std; 8166493378 2012-05-12 kinaba: typedef long long LL; 8166493378 2012-05-12 kinaba: typedef complex<double> CMP; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: struct UnionFind 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: vector<int> uf, sz; 8166493378 2012-05-12 kinaba: int nc; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: UnionFind(int N): uf(N), sz(N,1), nc(N) 8166493378 2012-05-12 kinaba: { for(int i=0; i<N; ++i) uf[i] = i; } 8166493378 2012-05-12 kinaba: int size() 8166493378 2012-05-12 kinaba: { return nc; } 8166493378 2012-05-12 kinaba: int Find(int a) 8166493378 2012-05-12 kinaba: { return uf[a]==a ? a : uf[a]=Find(uf[a]); } 8166493378 2012-05-12 kinaba: bool Union(int a, int b) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: a = Find(a); 8166493378 2012-05-12 kinaba: b = Find(b); 8166493378 2012-05-12 kinaba: if( a != b ) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: if( sz[a] >= sz[b] ) swap(a, b); 8166493378 2012-05-12 kinaba: uf[a] = b; 8166493378 2012-05-12 kinaba: sz[b] += sz[a]; 8166493378 2012-05-12 kinaba: --nc; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: return (a!=b); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: }; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: class EllysLights { public: 8166493378 2012-05-12 kinaba: int getMinimum(string initial, vector <string> switches) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: int N = switches.size(); 8166493378 2012-05-12 kinaba: UnionFind uf(N*2+2); 8166493378 2012-05-12 kinaba: #define TR 0 8166493378 2012-05-12 kinaba: #define FL 1 8166493378 2012-05-12 kinaba: #define POS(x) ((x)+2) 8166493378 2012-05-12 kinaba: #define NEG(x) ((x)+N+2) 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: for(int i=0; i<initial.size(); ++i) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: vector<int> sw; 8166493378 2012-05-12 kinaba: for(int k=0; k<N; ++k) 8166493378 2012-05-12 kinaba: if( switches[k][i]=='Y' ) 8166493378 2012-05-12 kinaba: sw.push_back(k); 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: if(initial[i] == 'Y') 8166493378 2012-05-12 kinaba: if(sw.size() == 0) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: return -1; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: else if(sw.size() == 1) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: uf.Union(POS(sw[0]), TR); 8166493378 2012-05-12 kinaba: uf.Union(NEG(sw[0]), FL); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: else 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: uf.Union(POS(sw[0]), NEG(sw[1])); 8166493378 2012-05-12 kinaba: uf.Union(NEG(sw[0]), POS(sw[1])); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: else 8166493378 2012-05-12 kinaba: if(sw.size() == 0) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: else if(sw.size() == 1) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: uf.Union(POS(sw[0]), FL); 8166493378 2012-05-12 kinaba: uf.Union(NEG(sw[0]), TR); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: else 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: uf.Union(POS(sw[0]), POS(sw[1])); 8166493378 2012-05-12 kinaba: uf.Union(NEG(sw[0]), NEG(sw[1])); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: if( uf.Find(TR) == uf.Find(FL) ) 8166493378 2012-05-12 kinaba: return -1; 8166493378 2012-05-12 kinaba: for(int k=0; k<N; ++k) 8166493378 2012-05-12 kinaba: if( uf.Find(POS(k)) == uf.Find(NEG(k)) ) 8166493378 2012-05-12 kinaba: return -1; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: int minSwitch = 0; 8166493378 2012-05-12 kinaba: set<int> done; 8166493378 2012-05-12 kinaba: for(int k=0; k<N; ++k) 8166493378 2012-05-12 kinaba: if( !done.count(k) ) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: vector<int> pos(1, k); 8166493378 2012-05-12 kinaba: vector<int> neg; 8166493378 2012-05-12 kinaba: for(int j=k+1; j<N; ++j) 8166493378 2012-05-12 kinaba: if( !done.count(j) ) 8166493378 2012-05-12 kinaba: if( uf.Find(POS(k)) == uf.Find(POS(j)) ) { 8166493378 2012-05-12 kinaba: pos.push_back(j); 8166493378 2012-05-12 kinaba: done.insert(j); 8166493378 2012-05-12 kinaba: } else if( uf.Find(POS(k)) == uf.Find(NEG(j)) ) { 8166493378 2012-05-12 kinaba: neg.push_back(j); 8166493378 2012-05-12 kinaba: done.insert(j); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: if( uf.Find(POS(k)) == uf.Find(TR) ) { 8166493378 2012-05-12 kinaba: minSwitch += pos.size(); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: else if( uf.Find(POS(k)) == uf.Find(FL) ) { 8166493378 2012-05-12 kinaba: minSwitch += neg.size(); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: else { 8166493378 2012-05-12 kinaba: minSwitch += min(pos.size(), neg.size()); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: return minSwitch; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: }; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: // BEGIN CUT HERE 8166493378 2012-05-12 kinaba: #include <ctime> 8166493378 2012-05-12 kinaba: double start_time; string timer() 8166493378 2012-05-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 8166493378 2012-05-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 8166493378 2012-05-12 kinaba: { os << "{ "; 8166493378 2012-05-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 8166493378 2012-05-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 8166493378 2012-05-12 kinaba: void verify_case(const int& Expected, const int& Received) { 8166493378 2012-05-12 kinaba: bool ok = (Expected == Received); 8166493378 2012-05-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 8166493378 2012-05-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 8166493378 2012-05-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 8166493378 2012-05-12 kinaba: #define END verify_case(_, EllysLights().getMinimum(initial, switches));} 8166493378 2012-05-12 kinaba: int main(){ 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: CASE(0) 8166493378 2012-05-12 kinaba: string initial = "YNYNNN"; 8166493378 2012-05-12 kinaba: string switches_[] = {"YNNYNY", "NYYYNN", "YNYNYN", "NNNNYN", "NYNNNY"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: int _ = 2; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(1) 8166493378 2012-05-12 kinaba: string initial = "YNYNYN"; 8166493378 2012-05-12 kinaba: string switches_[] = {"NNNNNN", "YYYYYY", "NYNNNN", "NNNYNN", "NNNNNY"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: int _ = 4; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(2) 8166493378 2012-05-12 kinaba: string initial = "YYN"; 8166493378 2012-05-12 kinaba: string switches_[] = {"YNY", "NYN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: int _ = -1; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(3) 8166493378 2012-05-12 kinaba: string initial = "NNYNYNYYYNNYYYYN"; 8166493378 2012-05-12 kinaba: string switches_[] = {"NYNYNYNYNYNYNYNY", 8166493378 2012-05-12 kinaba: "YNYNYNYNYNYNYNYN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "YNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NYNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNYNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNYNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNYNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNYNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNYNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNYNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNYNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNYNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNYNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNYNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNYNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNYNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNYN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNY"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: int _ = 6; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(4) 8166493378 2012-05-12 kinaba: string initial = "NYNYNYYYNNYYYNNYNNYYYYYNNYNYYYY"; 8166493378 2012-05-12 kinaba: string switches_[] = {"NNNNNNNNNNNNNNNNNNYNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNYNNNYNNNNYYNYNNNNYNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNYNNNNNNNNNNNNYNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNYNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NYNNNNNNNNNNNNYNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNYYNNNNNNNNNNNNNNNY", 8166493378 2012-05-12 kinaba: "NNNNNNYNNNNNNNNNNNNYNNNNNYNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "YNNNNNNNNNNNNNNNNNNYNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNYNNNNNNNNNNNNNNNNNNNYYNNNNNN", 8166493378 2012-05-12 kinaba: "NYNNNNNNNNNNYNNNNNNNNNNNNNNNYNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNYNNNNNNNNNNNNNNNNNNNNNNNNY", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNYNNNNNNNNNYYYNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNYNNNNNNNNNNNNNNNNNNNNNNNYNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNYNNNNNNNNNNNNNNNYNNNNNN", 8166493378 2012-05-12 kinaba: "YNNNYNNNNNNNNNNNNNNNNNNNNNNYNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNYNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNYNNYNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNYNNNYNNNYNNNNNNNNNNNNNYN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: int _ = 7; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(5) 8166493378 2012-05-12 kinaba: string initial = "NYNYYNYNYYYYNNYNYNNYYNNNNNYNYNNNNNYNNNYN"; 8166493378 2012-05-12 kinaba: string switches_[] = {"NNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNYNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNYNNNNYNNYNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNYNNNNYNNNNNNNYNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNYNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNYNNNNNNNNYNNNYNNNNNYNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNYNNNNNNNNNNNNNNYNNNNNYNNYNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNYNNYNNYNNNNNNNNNNNNNNNNNNNNNYNNNNNN", 8166493378 2012-05-12 kinaba: "YNNNYNNNNNNNNNNNNNYNNNYNNYNNNNNNNYNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNYYNNNNNNNNNNNNYNNNNYNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNNNY", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNYNNNNNNNNNNNYNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNYNNNNNNNNNNNNNNNNNYNNNNNNNNNNYNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNYNNYNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYNNY", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNYNNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNN", 8166493378 2012-05-12 kinaba: "NYNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NYNNNNYNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNYNNYYNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNYNNNNNNNNNNNNNNNNYYNNNNNNNNNNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNYNNNNNNNNNNNNNNNNNNNYNYNNNNNNNNN", 8166493378 2012-05-12 kinaba: "NNNNNNNNNNNNNNNNNNNNNNNNNNYNNYNNNNNNYNNN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: int _ = -1; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: /* 8166493378 2012-05-12 kinaba: CASE(6) 8166493378 2012-05-12 kinaba: string initial = ; 8166493378 2012-05-12 kinaba: string switches_[] = ; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: int _ = ; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(7) 8166493378 2012-05-12 kinaba: string initial = ; 8166493378 2012-05-12 kinaba: string switches_[] = ; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: int _ = ; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: */ 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: // END CUT HERE