15bfca2432 2012-01-31 kinaba: #include <iostream> 15bfca2432 2012-01-31 kinaba: #include <sstream> 15bfca2432 2012-01-31 kinaba: #include <iomanip> 15bfca2432 2012-01-31 kinaba: #include <vector> 15bfca2432 2012-01-31 kinaba: #include <string> 15bfca2432 2012-01-31 kinaba: #include <map> 15bfca2432 2012-01-31 kinaba: #include <set> 15bfca2432 2012-01-31 kinaba: #include <algorithm> 15bfca2432 2012-01-31 kinaba: #include <numeric> 15bfca2432 2012-01-31 kinaba: #include <iterator> 15bfca2432 2012-01-31 kinaba: #include <functional> 15bfca2432 2012-01-31 kinaba: #include <complex> 15bfca2432 2012-01-31 kinaba: #include <queue> 15bfca2432 2012-01-31 kinaba: #include <stack> 15bfca2432 2012-01-31 kinaba: #include <cmath> 15bfca2432 2012-01-31 kinaba: #include <cassert> 15bfca2432 2012-01-31 kinaba: #include <cstring> 15bfca2432 2012-01-31 kinaba: #ifdef __GNUC__ 15bfca2432 2012-01-31 kinaba: #include <ext/hash_map> 15bfca2432 2012-01-31 kinaba: #define unordered_map __gnu_cxx::hash_map 15bfca2432 2012-01-31 kinaba: #else 15bfca2432 2012-01-31 kinaba: #include <unordered_map> 15bfca2432 2012-01-31 kinaba: #endif 15bfca2432 2012-01-31 kinaba: using namespace std; 15bfca2432 2012-01-31 kinaba: typedef long long LL; 15bfca2432 2012-01-31 kinaba: typedef complex<double> CMP; 15bfca2432 2012-01-31 kinaba: 15bfca2432 2012-01-31 kinaba: class GogoXReimuHakurai { public: 15bfca2432 2012-01-31 kinaba: int solve(vector <string> choices) 15bfca2432 2012-01-31 kinaba: { 15bfca2432 2012-01-31 kinaba: set<int> fromSrc; 15bfca2432 2012-01-31 kinaba: { 15bfca2432 2012-01-31 kinaba: queue<int> q; 15bfca2432 2012-01-31 kinaba: q.push(0); 15bfca2432 2012-01-31 kinaba: fromSrc.insert(0); 15bfca2432 2012-01-31 kinaba: while( !q.empty() ) { 15bfca2432 2012-01-31 kinaba: int v = q.front(); 15bfca2432 2012-01-31 kinaba: q.pop(); 15bfca2432 2012-01-31 kinaba: for(int u=0; u<choices[v].size(); ++u) 15bfca2432 2012-01-31 kinaba: if( choices[v][u] == 'Y' && !fromSrc.count(u) ) 15bfca2432 2012-01-31 kinaba: q.push(u), fromSrc.insert(u); 15bfca2432 2012-01-31 kinaba: } 15bfca2432 2012-01-31 kinaba: } 15bfca2432 2012-01-31 kinaba: set<int> toDest; 15bfca2432 2012-01-31 kinaba: { 15bfca2432 2012-01-31 kinaba: queue<int> q; 15bfca2432 2012-01-31 kinaba: q.push(choices.size()-1); 15bfca2432 2012-01-31 kinaba: toDest.insert(choices.size()-1); 15bfca2432 2012-01-31 kinaba: while( !q.empty() ) { 15bfca2432 2012-01-31 kinaba: int v = q.front(); 15bfca2432 2012-01-31 kinaba: q.pop(); 15bfca2432 2012-01-31 kinaba: for(int u=0; u<choices[v].size(); ++u) 15bfca2432 2012-01-31 kinaba: if( choices[u][v] == 'Y' && !toDest.count(u) ) 15bfca2432 2012-01-31 kinaba: q.push(u), toDest.insert(u); 15bfca2432 2012-01-31 kinaba: } 15bfca2432 2012-01-31 kinaba: } 15bfca2432 2012-01-31 kinaba: set<int> valid; 15bfca2432 2012-01-31 kinaba: set_intersection(fromSrc.begin(), fromSrc.end(), 15bfca2432 2012-01-31 kinaba: toDest.begin(), toDest.end(), 15bfca2432 2012-01-31 kinaba: inserter(valid, valid.begin())); 15bfca2432 2012-01-31 kinaba: int N = valid.size(); 15bfca2432 2012-01-31 kinaba: int E = 0; 15bfca2432 2012-01-31 kinaba: for(int v=0; v<choices.size(); ++v) if(valid.count(v)) 15bfca2432 2012-01-31 kinaba: for(int u=0; u<choices.size(); ++u) if(v!=u && valid.count(u)) 15bfca2432 2012-01-31 kinaba: if( choices[v][u]=='Y' ) 15bfca2432 2012-01-31 kinaba: ++E; 15bfca2432 2012-01-31 kinaba: return N ? E - (N-2) : 0; 15bfca2432 2012-01-31 kinaba: } 15bfca2432 2012-01-31 kinaba: }; 15bfca2432 2012-01-31 kinaba: 15bfca2432 2012-01-31 kinaba: // BEGIN CUT HERE 15bfca2432 2012-01-31 kinaba: #include <ctime> 15bfca2432 2012-01-31 kinaba: double start_time; string timer() 15bfca2432 2012-01-31 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 15bfca2432 2012-01-31 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 15bfca2432 2012-01-31 kinaba: { os << "{ "; 15bfca2432 2012-01-31 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 15bfca2432 2012-01-31 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 15bfca2432 2012-01-31 kinaba: void verify_case(const int& Expected, const int& Received) { 15bfca2432 2012-01-31 kinaba: bool ok = (Expected == Received); 15bfca2432 2012-01-31 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 15bfca2432 2012-01-31 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 15bfca2432 2012-01-31 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 15bfca2432 2012-01-31 kinaba: #define END verify_case(_, GogoXReimuHakurai().solve(choices));} 15bfca2432 2012-01-31 kinaba: int main(){ 15bfca2432 2012-01-31 kinaba: 15bfca2432 2012-01-31 kinaba: CASE(0) 15bfca2432 2012-01-31 kinaba: string choices_[] = { 15bfca2432 2012-01-31 kinaba: "NYY", 15bfca2432 2012-01-31 kinaba: "NNY", 15bfca2432 2012-01-31 kinaba: "NNN"}; 15bfca2432 2012-01-31 kinaba: vector <string> choices(choices_, choices_+sizeof(choices_)/sizeof(*choices_)); 15bfca2432 2012-01-31 kinaba: int _ = 2; 15bfca2432 2012-01-31 kinaba: END 15bfca2432 2012-01-31 kinaba: CASE(1) 15bfca2432 2012-01-31 kinaba: string choices_[] = { 15bfca2432 2012-01-31 kinaba: "NYNY", 15bfca2432 2012-01-31 kinaba: "NNYY", 15bfca2432 2012-01-31 kinaba: "NNNY", 15bfca2432 2012-01-31 kinaba: "NNNN"}; 15bfca2432 2012-01-31 kinaba: vector <string> choices(choices_, choices_+sizeof(choices_)/sizeof(*choices_)); 15bfca2432 2012-01-31 kinaba: int _ = 3; 15bfca2432 2012-01-31 kinaba: END 15bfca2432 2012-01-31 kinaba: CASE(2) 15bfca2432 2012-01-31 kinaba: string choices_[] = {"NNY" 15bfca2432 2012-01-31 kinaba: ,"NNY" 15bfca2432 2012-01-31 kinaba: ,"NNN"}; 15bfca2432 2012-01-31 kinaba: vector <string> choices(choices_, choices_+sizeof(choices_)/sizeof(*choices_)); 15bfca2432 2012-01-31 kinaba: int _ = 1; 15bfca2432 2012-01-31 kinaba: END 15bfca2432 2012-01-31 kinaba: CASE(3) 15bfca2432 2012-01-31 kinaba: string choices_[] = {"NN" 15bfca2432 2012-01-31 kinaba: ,"NN"}; 15bfca2432 2012-01-31 kinaba: vector <string> choices(choices_, choices_+sizeof(choices_)/sizeof(*choices_)); 15bfca2432 2012-01-31 kinaba: int _ = 0; 15bfca2432 2012-01-31 kinaba: END 15bfca2432 2012-01-31 kinaba: /* 15bfca2432 2012-01-31 kinaba: CASE(4) 15bfca2432 2012-01-31 kinaba: string choices_[] = ; 15bfca2432 2012-01-31 kinaba: vector <string> choices(choices_, choices_+sizeof(choices_)/sizeof(*choices_)); 15bfca2432 2012-01-31 kinaba: int _ = ; 15bfca2432 2012-01-31 kinaba: END 15bfca2432 2012-01-31 kinaba: CASE(5) 15bfca2432 2012-01-31 kinaba: string choices_[] = ; 15bfca2432 2012-01-31 kinaba: vector <string> choices(choices_, choices_+sizeof(choices_)/sizeof(*choices_)); 15bfca2432 2012-01-31 kinaba: int _ = ; 15bfca2432 2012-01-31 kinaba: END 15bfca2432 2012-01-31 kinaba: */ 15bfca2432 2012-01-31 kinaba: } 15bfca2432 2012-01-31 kinaba: // END CUT HERE