Artifact Content
Not logged in

Artifact bf3b5407a7b91a66e6ad23cc64574ce0e434b058


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

typedef int           vert;
typedef vert          edge;
typedef vector<edge>  edges;
typedef vector<edges> graph;

bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] )
{
	for(int i=0; i<G[v].size(); ++i) {
		vert u = G[v][i];
		if( visited[u] ) continue;
		visited[u] = true;

		if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) )
			{ matchTo[v]=u, matchTo[u]=v; return true; }
	}
	return false;
}

template<int NV>
int biMatch( graph& G, int L ) 
{
	vector<vert> matchTo(G.size(), -1);
	int ans = 0;
	for(vert v=0; v<L; ++v) {
		bool visited[NV] = {};
		if( augment(G, v, matchTo, visited) )
			++ans;
	}
	return ans;
}

class SwitchesAndLamps { public:
	int theMin(vector <string> switches, vector <string> lamps)
	{
		const int N = switches[0].size();
		const int E = switches.size();

		vector< vector<bool> > uneq(N, vector<bool>(N));

		vector<LL> id(N, 1);
		for(int e=0; e<E; ++e) {
			for(int s=0; s<N; ++s) {
				id[s] = id[s]*2 + (switches[e][s]=='Y' ? 0 : 1);
				for(int l=0; l<N; ++l) {
					if( switches[e][s] != lamps[e][l] )
						uneq[s][l] = true;
				}
			}
		}

		graph G(N*2);
		for(int s=0; s<N; ++s)
			for(int l=0; l<N; ++l)
				if( !uneq[s][l] )
					G[s].push_back(N+l);
		if( N > biMatch<100>(G, N) )
			return -1;

		map<LL, int> sizes;
		int maxSize = 1;
		for(int s=0; s<N; ++s)
			maxSize = max(maxSize, ++sizes[id[s]]);
		return calcNeed(maxSize);
	}

	int calcNeed(int maxSize)
	{
		if( maxSize<=1 )
			return 0;
		return 1 + calcNeed((maxSize+1)/2);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, SwitchesAndLamps().theMin(switches, lamps));}
int main(){

CASE(0)
	string switches_[] = {"NYNN", "NNYN"};
	  vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 
	string lamps_[] = {"NNNY", "NNYN"};
	  vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 
	int _ = 1; 
END
CASE(1)
	string switches_[] = {"YYN", "YNY", "YYN"};
	  vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 
	string lamps_[] = {"YNY", "NYY", "YNY"};
	  vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 
	int _ = 0; 
END
CASE(2)
	string switches_[] = {"NYYYNYNNYYYNYNNNNY",
	                      "NYYYNYNNYYYNYNNNNY"};
	  vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 
	string lamps_[] =    {"YYYNNNYNNYNYNYNYNY",
		                  "YYYNNNYNNYNYYNNYNY"};
	  vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 
	int _ = -1; 
END
CASE(3)
	string switches_[] = {"YYNNN", "NNYYN"};
	  vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 
	string lamps_[] =    {"NYNNY", "NNNYY"};
	  vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 
	int _ = -1; 
END
CASE(4)
	string switches_[] = {"YNNYNNYNYY", "NYNNYNYNYN", "YNYNYYYYYN", "NNYYNYNYNN"};
	  vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 
	string lamps_[] = {"NYYNYNNYNY", "NYYYNNYNNN", "YYYYNYNNYY", "YNNNNYNYYN"};
	  vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 
	int _ = 2; 
END
CASE(5)
string switches_[] = {"YYNNN", "YNNNN", "NYNNN"};
	  vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 
string lamps_[] = {"YYNNN", "YNNNN", "NNYNN"};
	  vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 
	int _ = -1; 
END
CASE(6)
string switches_[] = {"YYNNN", "YNNNN", "NYNNN"};
	  vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 
string lamps_[] = {"YYNNN", "YNNNN", "YNNNN"};
	  vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 
	int _ = -1; 
END
/*
CASE(6)
	string switches_[] = ;
	  vector <string> switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); 
	string lamps_[] = ;
	  vector <string> lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); 
	int _ = ; 
END
*/
}
// END CUT HERE