Artifact Content
Not logged in

Artifact 1cfc045ea674117fa1da69d684022ad8eae0bac0


#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;

class Rumor { public:
	int getMinimum(string knowledge, vector <string> graph)
	{
		const int N = knowledge.size();
		const int ALL = (1<<N) - 1;

		// Turn the input into bit-vectors.
		int know=0;
		for(int v=0; v<N; ++v)
			know |= (knowledge[v]=='Y') << v;
		vector<int> g(N);
		for(int v=0; v<N; ++v)
		for(int u=0; u<N; ++u)
			g[v] |= (graph[v][u]=='Y') << u;

		// Try all possible patterns (which rumor each rabit spreads first when she knows both), and simulate.
		int best = N+1;
		for(int preferA=0; preferA<(1<<N); ++preferA)
			for(int knowA=know, knowB=know, didA=0, didB=0, step=0; step<best; ++step) {
				if( knowA==ALL && knowB==ALL )
					{best=step; break;}

				int doA=0, doB=0;
				for(int v=0; v<N; ++v) {
					bool a_ok = (knowA & 1<<v) && !(didA & 1<<v);
					bool b_ok = (knowB & 1<<v) && !(didB & 1<<v);
					doA |= (a_ok && b_ok && (preferA & 1<<v) || a_ok && !b_ok) << v;
					doB |= (a_ok && b_ok && !(preferA & 1<<v) || !a_ok && b_ok) << v;
				}
				didA |= doA;
				didB |= doB;
				for(int v=0; v<N; ++v) {
					if(doA & 1<<v) knowA |= g[v];
					if(doB & 1<<v) knowB |= g[v];
				}
			}
		return best==N+1 ? -1 : best;
	}
};

// 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(_, Rumor().getMinimum(knowledge, graph));}
int main(){

CASE(0)
	string knowledge = "YNN"; 
	string graph_[] = {"NYN"
,"NNY"
,"NNN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 3; 
END
CASE(1)
	string knowledge = "YNNY"; 
	string graph_[] = {"NYYN"
,"YNNY"
,"YNNY"
,"NYYN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 1; 
END
CASE(2)
	string knowledge = "YYYY"; 
	string graph_[] = {"NYNN"
,"YNYN"
,"NYNY"
,"NNYN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 0; 
END
CASE(3)
	string knowledge = "YYYYYN"; 
	string graph_[] = {"NYYYYN"
,"YNYYYN"
,"YYNYYN"
,"YYYNYN"
,"YYYYNN"
,"NNNNNN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = -1; 
END
CASE(4)
	string knowledge = "NNNY"; 
	string graph_[] = {"NNNN"
,"YNNN"
,"YNNN"
,"NYYN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 3; 
END
CASE(5)
	string knowledge =  "NNNNNNNYYY"; 
	string graph_[] = {"NYNNYNNYNN"
,"NNYNYNNNNY"
,"YYNNNYNNNN"
,"YNNNYNYNNN"
,"NNYNNYNNYN"
,"NNNNYNNNYY"
,"NYNYNYNNNN"
,"NNNNNNYNYY"
,"NNNYNNNYNY"
,"NYYNNNNYNN"}
;
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 2; 
END
CASE(6)
string knowledge = "YYYNNN"; 
	string graph_[] = {
		"NNNYNY",
		"NNNYYN",
		"NNNNYY",
		"NNNNNN",
		"NNNNNN",
		"NNNNNN"
	};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 2; 
END
/*
CASE(7)
	string knowledge = ; 
	string graph_[] = ;
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = ; 
END
*/
}
// END CUT HERE