Artifact Content
Not logged in

Artifact 3f7b3fa46030db3fc02a6a3cd3ac340ff87a442a


#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>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class CarrotBoxes { public:
	double theProbability(vector <string> info)
	{
		info = warshall_floyd(info);

		double answer=0.0, p=1.0;

		set<int> boxes;
		for(int v=0; v<info.size(); ++v)
			boxes.insert(v);

		while( !boxes.empty() )
		{
			for(set<int>::iterator it=boxes.begin(); it!=boxes.end(); ++it)
				if( isRoot(*it, info, boxes) && !isSingleton(*it, info, boxes) )
				{
					int N = boxes.size();
					int nr = 0;
					for(int u=0; u<info.size(); ++u)
						if( info[*it][u]=='Y' && boxes.count(u) )
							{++nr; boxes.erase(u);}
					answer += p * (nr-1) / N;
					p *= double(N-nr) / N;
					goto nextIter;
				}

			// ‘S•”‚ª stand alone
			answer += p / boxes.size();
			break;

		nextIter:;
		}
		return answer;
	}

	bool isRoot(int v, const vector<string>& info, const set<int>& boxes)
	{
		for(set<int>::const_iterator jt=boxes.begin(); jt!=boxes.end(); ++jt)
			if( info[v][*jt]=='N' && info[*jt][v]=='Y' )
				return false;
		return true;
	}

	bool isSingleton(int v, const vector<string>& info, const set<int>& boxes)
	{
		for(set<int>::const_iterator jt=boxes.begin(); jt!=boxes.end(); ++jt)
			if( v!=*jt && info[v][*jt]=='Y' )
				return false;
		return true;
	}

	vector<string> warshall_floyd(const vector<string>& gg)
	{
		vector<string> g = gg;
		for(int k=0; k<g.size(); ++k)
			for(int i=0; i<g.size(); ++i)
				for(int j=0; j<g.size(); ++j)
					if( g[i][k]=='Y' && g[k][j]=='Y' )
						g[i][j] = 'Y';
		return g;
	}
};

// 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 double& Expected, const double& Received) {
 bool ok = (abs(Expected - Received) < 1e-9);
 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(_, CarrotBoxes().theProbability(information));}
int main(){

CASE(0)
	string information_[] = {"YYYYY",
 "NYNNN",
 "NNYNN",
 "NNNYN",
 "NNNNY"}
;
	  vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 
	double _ = 0.8; 
END
CASE(1)
	string information_[] = {"YNNNN",
 "NYNNN",
 "NNYNN",
 "NNNYN",
 "NNNNY"};
	  vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 
	double _ = 0.2; 
END
CASE(2)
	string information_[] = {"Y"};
	  vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 
	double _ = 1.0; 
END
CASE(3)
	string information_[] = {"YNNNN",
 "YYNNN",
 "YNYNN",
 "NNNYY",
 "NNNYY"}
;
	  vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 
	double _ = 0.6; 
END
CASE(4)
	string information_[] = {"YYYNNNYN",
 "NYNNNNYN",
 "NNYNNNNN",
 "NYNYNNNN",
 "YNNNYNNY",
 "NNYNNYNN",
 "NNNNYNYN",
 "NNYNNNNY"}
;
	  vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 
	double _ = 0.875; 
END
CASE(5)
	string information_[] = {"YNNNNNNNNYNNNNNNNNNN",
 "NYNNNNNNNNNNNNNNNNNN",
 "NNYNNNNNNNYNNNNNYNNN",
 "NNNYNYNNNNNNNNYNNNNN",
 "NNNNYNNNNNNNNNYNNNNY",
 "NNNNNYNNNNNNNNNNNNNY",
 "NNNNYNYNYNNNNNNNNNNN",
 "NNNNNNNYNNNYYNNNNNNN",
 "NNNNNNNNYNNNNNNNNNNN",
 "YNNNNNNNNYNNNNNYNNNN",
 "NNNNNNNNNNYNNNNNNNNN",
 "NYNNNNNNNNNYNNNNNNNN",
 "NNNNNNNYNNNNYNNNNNNN",
 "NNNNNNNNNNNNNYNNNYNN",
 "NNNNNNNNNNNYNNYNNNYN",
 "NYNNNNNNNNNNNNNYNNNN",
 "NNYNNNNNNNNNNNNNYNNN",
 "NNNNNNNNNNNNNYNYNYNN",
 "NNNNNNNNYNYNNNNNNNYY",
 "NNNYNNNNNNNNNNNNNNNY"};
	  vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 
	double _ = 0.75; 
END
/*
CASE(6)
	string information_[] = ;
	  vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 
	double _ = ; 
END
CASE(7)
	string information_[] = ;
	  vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 
	double _ = ; 
END
*/
}
// END CUT HERE