4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class CarrotBoxes { public: 4fd800b3a8 2011-02-23 kinaba: double theProbability(vector <string> info) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: info = warshall_floyd(info); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double answer=0.0, p=1.0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: set<int> boxes; 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<info.size(); ++v) 4fd800b3a8 2011-02-23 kinaba: boxes.insert(v); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: while( !boxes.empty() ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(set<int>::iterator it=boxes.begin(); it!=boxes.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: if( isRoot(*it, info, boxes) && !isSingleton(*it, info, boxes) ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = boxes.size(); 4fd800b3a8 2011-02-23 kinaba: int nr = 0; 4fd800b3a8 2011-02-23 kinaba: for(int u=0; u<info.size(); ++u) 4fd800b3a8 2011-02-23 kinaba: if( info[*it][u]=='Y' && boxes.count(u) ) 4fd800b3a8 2011-02-23 kinaba: {++nr; boxes.erase(u);} 4fd800b3a8 2011-02-23 kinaba: answer += p * (nr-1) / N; 4fd800b3a8 2011-02-23 kinaba: p *= double(N-nr) / N; 4fd800b3a8 2011-02-23 kinaba: goto nextIter; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // ‘S•”‚ª stand alone 4fd800b3a8 2011-02-23 kinaba: answer += p / boxes.size(); 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: nextIter:; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return answer; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool isRoot(int v, const vector<string>& info, const set<int>& boxes) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(set<int>::const_iterator jt=boxes.begin(); jt!=boxes.end(); ++jt) 4fd800b3a8 2011-02-23 kinaba: if( info[v][*jt]=='N' && info[*jt][v]=='Y' ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool isSingleton(int v, const vector<string>& info, const set<int>& boxes) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(set<int>::const_iterator jt=boxes.begin(); jt!=boxes.end(); ++jt) 4fd800b3a8 2011-02-23 kinaba: if( v!=*jt && info[v][*jt]=='Y' ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<string> warshall_floyd(const vector<string>& gg) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<string> g = gg; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<g.size(); ++k) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<g.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<g.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( g[i][k]=='Y' && g[k][j]=='Y' ) 4fd800b3a8 2011-02-23 kinaba: g[i][j] = 'Y'; 4fd800b3a8 2011-02-23 kinaba: return g; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const double& Expected, const double& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, CarrotBoxes().theProbability(information));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string information_[] = {"YYYYY", 4fd800b3a8 2011-02-23 kinaba: "NYNNN", 4fd800b3a8 2011-02-23 kinaba: "NNYNN", 4fd800b3a8 2011-02-23 kinaba: "NNNYN", 4fd800b3a8 2011-02-23 kinaba: "NNNNY"} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.8; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string information_[] = {"YNNNN", 4fd800b3a8 2011-02-23 kinaba: "NYNNN", 4fd800b3a8 2011-02-23 kinaba: "NNYNN", 4fd800b3a8 2011-02-23 kinaba: "NNNYN", 4fd800b3a8 2011-02-23 kinaba: "NNNNY"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.2; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string information_[] = {"Y"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 1.0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string information_[] = {"YNNNN", 4fd800b3a8 2011-02-23 kinaba: "YYNNN", 4fd800b3a8 2011-02-23 kinaba: "YNYNN", 4fd800b3a8 2011-02-23 kinaba: "NNNYY", 4fd800b3a8 2011-02-23 kinaba: "NNNYY"} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.6; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string information_[] = {"YYYNNNYN", 4fd800b3a8 2011-02-23 kinaba: "NYNNNNYN", 4fd800b3a8 2011-02-23 kinaba: "NNYNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NYNYNNNN", 4fd800b3a8 2011-02-23 kinaba: "YNNNYNNY", 4fd800b3a8 2011-02-23 kinaba: "NNYNNYNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNYNYN", 4fd800b3a8 2011-02-23 kinaba: "NNYNNNNY"} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.875; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: string information_[] = {"YNNNNNNNNYNNNNNNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NYNNNNNNNNNNNNNNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NNYNNNNNNNYNNNNNYNNN", 4fd800b3a8 2011-02-23 kinaba: "NNNYNYNNNNNNNNYNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNYNNNNNNNNNYNNNNY", 4fd800b3a8 2011-02-23 kinaba: "NNNNNYNNNNNNNNNNNNNY", 4fd800b3a8 2011-02-23 kinaba: "NNNNYNYNYNNNNNNNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNNNNYNNNYYNNNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNNNNNYNNNNNNNNNNN", 4fd800b3a8 2011-02-23 kinaba: "YNNNNNNNNYNNNNNYNNNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNNNNNNNYNNNNNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NYNNNNNNNNNYNNNNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNNNNYNNNNYNNNNNNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNNNNNNNNNNYNNNYNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNNNNNNNNYNNYNNNYN", 4fd800b3a8 2011-02-23 kinaba: "NYNNNNNNNNNNNNNYNNNN", 4fd800b3a8 2011-02-23 kinaba: "NNYNNNNNNNNNNNNNYNNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNNNNNNNNNNYNYNYNN", 4fd800b3a8 2011-02-23 kinaba: "NNNNNNNNYNYNNNNNNNYY", 4fd800b3a8 2011-02-23 kinaba: "NNNYNNNNNNNNNNNNNNNY"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.75; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: string information_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 4fd800b3a8 2011-02-23 kinaba: double _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(7) 4fd800b3a8 2011-02-23 kinaba: string information_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> information(information_, information_+sizeof(information_)/sizeof(*information_)); 4fd800b3a8 2011-02-23 kinaba: double _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE