4cc63e9faa 2012-10-11 kinaba: #include <iostream> 4cc63e9faa 2012-10-11 kinaba: #include <sstream> 4cc63e9faa 2012-10-11 kinaba: #include <iomanip> 4cc63e9faa 2012-10-11 kinaba: #include <vector> 4cc63e9faa 2012-10-11 kinaba: #include <string> 4cc63e9faa 2012-10-11 kinaba: #include <map> 4cc63e9faa 2012-10-11 kinaba: #include <set> 4cc63e9faa 2012-10-11 kinaba: #include <algorithm> 4cc63e9faa 2012-10-11 kinaba: #include <numeric> 4cc63e9faa 2012-10-11 kinaba: #include <iterator> 4cc63e9faa 2012-10-11 kinaba: #include <functional> 4cc63e9faa 2012-10-11 kinaba: #include <complex> 4cc63e9faa 2012-10-11 kinaba: #include <queue> 4cc63e9faa 2012-10-11 kinaba: #include <stack> 4cc63e9faa 2012-10-11 kinaba: #include <cmath> 4cc63e9faa 2012-10-11 kinaba: #include <cassert> 4cc63e9faa 2012-10-11 kinaba: using namespace std; 4cc63e9faa 2012-10-11 kinaba: typedef long long LL; 4cc63e9faa 2012-10-11 kinaba: typedef long double LD; 4cc63e9faa 2012-10-11 kinaba: typedef complex<LD> CMP; 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: typedef int vert; 4cc63e9faa 2012-10-11 kinaba: typedef vert edge; 4cc63e9faa 2012-10-11 kinaba: typedef vector<edge> edges; 4cc63e9faa 2012-10-11 kinaba: typedef vector<edges> graph; 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) 4cc63e9faa 2012-10-11 kinaba: { 4cc63e9faa 2012-10-11 kinaba: for(int i=0; i<G[v].size(); ++i) { 4cc63e9faa 2012-10-11 kinaba: vert u = G[v][i]; 4cc63e9faa 2012-10-11 kinaba: if( visited[u] ) continue; 4cc63e9faa 2012-10-11 kinaba: visited[u] = true; 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) 4cc63e9faa 2012-10-11 kinaba: { matchTo[v]=u, matchTo[u]=v; return true; } 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: return false; 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: template<int NV> 4cc63e9faa 2012-10-11 kinaba: int biMatch( graph& G, int L ) // [0,L):left, [L,?):right 4cc63e9faa 2012-10-11 kinaba: // only left->right edges are used during computation 4cc63e9faa 2012-10-11 kinaba: { 4cc63e9faa 2012-10-11 kinaba: vector<vert> matchTo(G.size(), -1); 4cc63e9faa 2012-10-11 kinaba: int ans = 0; 4cc63e9faa 2012-10-11 kinaba: for(vert v=0; v<L; ++v) { 4cc63e9faa 2012-10-11 kinaba: bool visited[NV] = {}; 4cc63e9faa 2012-10-11 kinaba: if( augment(G, v, matchTo, visited) ) 4cc63e9faa 2012-10-11 kinaba: ++ans; 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: return ans; 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: class Incubator { public: 4cc63e9faa 2012-10-11 kinaba: int maxMagicalGirls(vector <string> love) 4cc63e9faa 2012-10-11 kinaba: { 4cc63e9faa 2012-10-11 kinaba: const int N = love.size(); 4cc63e9faa 2012-10-11 kinaba: vector< vector<int> > protects(N, vector<int>(N, false)); 4cc63e9faa 2012-10-11 kinaba: for(int i=0; i<N; ++i) 4cc63e9faa 2012-10-11 kinaba: for(int j=0; j<N; ++j) 4cc63e9faa 2012-10-11 kinaba: protects[i][j] = (love[i][j] == 'Y'); 4cc63e9faa 2012-10-11 kinaba: for(int k=0; k<N; ++k) 4cc63e9faa 2012-10-11 kinaba: for(int i=0; i<N; ++i) 4cc63e9faa 2012-10-11 kinaba: for(int j=0; j<N; ++j) 4cc63e9faa 2012-10-11 kinaba: protects[i][j] |= protects[i][k] & protects[k][j]; 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: int cand = N; 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: graph G(N*2); 4cc63e9faa 2012-10-11 kinaba: for(int i=0; i<N; ++i) 4cc63e9faa 2012-10-11 kinaba: if(protects[i][i]) { 4cc63e9faa 2012-10-11 kinaba: --cand; 4cc63e9faa 2012-10-11 kinaba: } else { 4cc63e9faa 2012-10-11 kinaba: for(int j=0; j<N; ++j) 4cc63e9faa 2012-10-11 kinaba: if(protects[i][j] && !protects[j][j]) 4cc63e9faa 2012-10-11 kinaba: G[i].push_back(N+j); 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: return cand - biMatch<512>(G, N); 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: }; 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: // BEGIN CUT HERE 4cc63e9faa 2012-10-11 kinaba: #include <ctime> 4cc63e9faa 2012-10-11 kinaba: double start_time; string timer() 4cc63e9faa 2012-10-11 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4cc63e9faa 2012-10-11 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4cc63e9faa 2012-10-11 kinaba: { os << "{ "; 4cc63e9faa 2012-10-11 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4cc63e9faa 2012-10-11 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4cc63e9faa 2012-10-11 kinaba: void verify_case(const int& Expected, const int& Received) { 4cc63e9faa 2012-10-11 kinaba: bool ok = (Expected == Received); 4cc63e9faa 2012-10-11 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4cc63e9faa 2012-10-11 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4cc63e9faa 2012-10-11 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4cc63e9faa 2012-10-11 kinaba: #define END verify_case(_, Incubator().maxMagicalGirls(love));} 4cc63e9faa 2012-10-11 kinaba: int main(){ 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: CASE(0) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"NY","NN"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 1; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(1) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"NYN", "NNY", "NNN"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 1; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(2) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"NNYNN","NNYNN","NNNYY","NNNNN","NNNNN"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 2; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(3) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"NNNNN","NYNNN","NYNYN","YNYNN","NNNNN"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 2; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(4) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"NNNNN","NNNNN","NNNNN","NNNNN","NNNNN"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 5; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(5) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"NNYNNNNN","NNNYNNNN","NNNNYNNN","NNYNNNNN","NNNNNYYN","NNNYNNNY","NNNNNNNN","NNNNNNNN"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 2; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(6) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"Y"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 0; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(7) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"NYNNN", "NNYNN", "NNYYY", "NNYYY", "NNYYY"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 1; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(8) 4cc63e9faa 2012-10-11 kinaba: string love_[] = {"NNYYYYY", "NNYYYYY", "NNNNNNN", "NNNYYYY", "NNNYYYY", "NNNYYYY", "NNNYYYY"}; 4cc63e9faa 2012-10-11 kinaba: vector <string> love(love_, love_+sizeof(love_)/sizeof(*love_)); 4cc63e9faa 2012-10-11 kinaba: int _ = 2; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: // END CUT HERE