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: typedef int vert; 8166493378 2012-05-12 kinaba: typedef vert edge; 8166493378 2012-05-12 kinaba: typedef vector<edge> edges; 8166493378 2012-05-12 kinaba: typedef vector<edges> graph; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: for(int i=0; i<G[v].size(); ++i) { 8166493378 2012-05-12 kinaba: vert u = G[v][i]; 8166493378 2012-05-12 kinaba: if( visited[u] ) continue; 8166493378 2012-05-12 kinaba: visited[u] = true; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) 8166493378 2012-05-12 kinaba: { matchTo[v]=u, matchTo[u]=v; return true; } 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: return false; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: template<int NV> 8166493378 2012-05-12 kinaba: int biMatch( graph& G, int L ) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: vector<vert> matchTo(G.size(), -1); 8166493378 2012-05-12 kinaba: int ans = 0; 8166493378 2012-05-12 kinaba: for(vert v=0; v<L; ++v) { 8166493378 2012-05-12 kinaba: bool visited[NV] = {}; 8166493378 2012-05-12 kinaba: if( augment(G, v, matchTo, visited) ) 8166493378 2012-05-12 kinaba: ++ans; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: return ans; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: class SwitchesAndLamps { public: 8166493378 2012-05-12 kinaba: int theMin(vector <string> switches, vector <string> lamps) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: const int N = switches[0].size(); 8166493378 2012-05-12 kinaba: const int E = switches.size(); 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: vector< vector<bool> > uneq(N, vector<bool>(N)); 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: vector<LL> id(N, 1); 8166493378 2012-05-12 kinaba: for(int e=0; e<E; ++e) { 8166493378 2012-05-12 kinaba: for(int s=0; s<N; ++s) { 8166493378 2012-05-12 kinaba: id[s] = id[s]*2 + (switches[e][s]=='Y' ? 0 : 1); 8166493378 2012-05-12 kinaba: for(int l=0; l<N; ++l) { 8166493378 2012-05-12 kinaba: if( switches[e][s] != lamps[e][l] ) 8166493378 2012-05-12 kinaba: uneq[s][l] = true; 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: graph G(N*2); 8166493378 2012-05-12 kinaba: for(int s=0; s<N; ++s) 8166493378 2012-05-12 kinaba: for(int l=0; l<N; ++l) 8166493378 2012-05-12 kinaba: if( !uneq[s][l] ) 8166493378 2012-05-12 kinaba: G[s].push_back(N+l); 8166493378 2012-05-12 kinaba: if( N > biMatch<100>(G, N) ) 8166493378 2012-05-12 kinaba: return -1; 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: map<LL, int> sizes; 8166493378 2012-05-12 kinaba: int maxSize = 1; 8166493378 2012-05-12 kinaba: for(int s=0; s<N; ++s) 8166493378 2012-05-12 kinaba: maxSize = max(maxSize, ++sizes[id[s]]); 8166493378 2012-05-12 kinaba: return calcNeed(maxSize); 8166493378 2012-05-12 kinaba: } 8166493378 2012-05-12 kinaba: 8166493378 2012-05-12 kinaba: int calcNeed(int maxSize) 8166493378 2012-05-12 kinaba: { 8166493378 2012-05-12 kinaba: if( maxSize<=1 ) 8166493378 2012-05-12 kinaba: return 0; 8166493378 2012-05-12 kinaba: return 1 + calcNeed((maxSize+1)/2); 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(_, SwitchesAndLamps().theMin(switches, lamps));} 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 switches_[] = {"NYNN", "NNYN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: string lamps_[] = {"NNNY", "NNYN"}; 8166493378 2012-05-12 kinaba: vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 8166493378 2012-05-12 kinaba: int _ = 1; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(1) 8166493378 2012-05-12 kinaba: string switches_[] = {"YYN", "YNY", "YYN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: string lamps_[] = {"YNY", "NYY", "YNY"}; 8166493378 2012-05-12 kinaba: vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 8166493378 2012-05-12 kinaba: int _ = 0; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(2) 8166493378 2012-05-12 kinaba: string switches_[] = {"NYYYNYNNYYYNYNNNNY", 8166493378 2012-05-12 kinaba: "NYYYNYNNYYYNYNNNNY"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: string lamps_[] = {"YYYNNNYNNYNYNYNYNY", 8166493378 2012-05-12 kinaba: "YYYNNNYNNYNYYNNYNY"}; 8166493378 2012-05-12 kinaba: vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 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 switches_[] = {"YYNNN", "NNYYN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: string lamps_[] = {"NYNNY", "NNNYY"}; 8166493378 2012-05-12 kinaba: vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 8166493378 2012-05-12 kinaba: int _ = -1; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(4) 8166493378 2012-05-12 kinaba: string switches_[] = {"YNNYNNYNYY", "NYNNYNYNYN", "YNYNYYYYYN", "NNYYNYNYNN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: string lamps_[] = {"NYYNYNNYNY", "NYYYNNYNNN", "YYYYNYNNYY", "YNNNNYNYYN"}; 8166493378 2012-05-12 kinaba: vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 8166493378 2012-05-12 kinaba: int _ = 2; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(5) 8166493378 2012-05-12 kinaba: string switches_[] = {"YYNNN", "YNNNN", "NYNNN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: string lamps_[] = {"YYNNN", "YNNNN", "NNYNN"}; 8166493378 2012-05-12 kinaba: vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 8166493378 2012-05-12 kinaba: int _ = -1; 8166493378 2012-05-12 kinaba: END 8166493378 2012-05-12 kinaba: CASE(6) 8166493378 2012-05-12 kinaba: string switches_[] = {"YYNNN", "YNNNN", "NYNNN"}; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: string lamps_[] = {"YYNNN", "YNNNN", "YNNNN"}; 8166493378 2012-05-12 kinaba: vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 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 switches_[] = ; 8166493378 2012-05-12 kinaba: vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 8166493378 2012-05-12 kinaba: string lamps_[] = ; 8166493378 2012-05-12 kinaba: vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 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