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: typedef int vert; 4fd800b3a8 2011-02-23 kinaba: typedef vert edge; 4fd800b3a8 2011-02-23 kinaba: typedef vector<edge> edges; 4fd800b3a8 2011-02-23 kinaba: typedef vector<edges> graph; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<G[v].size(); ++i) { 4fd800b3a8 2011-02-23 kinaba: vert u = G[v][i]; 4fd800b3a8 2011-02-23 kinaba: if( visited[u] ) continue; 4fd800b3a8 2011-02-23 kinaba: visited[u] = true; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) 4fd800b3a8 2011-02-23 kinaba: { matchTo[v]=u, matchTo[u]=v; return true; } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int NV> 4fd800b3a8 2011-02-23 kinaba: int biMatch( graph& G, int L ) // [0,L):left, [L,?):right 4fd800b3a8 2011-02-23 kinaba: // only left->right edges are used during computation 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<vert> matchTo(G.size(), -1); 4fd800b3a8 2011-02-23 kinaba: int ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(vert v=0; v<L; ++v) { 4fd800b3a8 2011-02-23 kinaba: bool visited[NV] = {}; 4fd800b3a8 2011-02-23 kinaba: if( augment(G, v, matchTo, visited) ) 4fd800b3a8 2011-02-23 kinaba: ++ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class CompanyRestructuring { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int fewestDivisions(vector <string> hasManaged) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N = hasManaged.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool hm[50][50], hmStar[50][50]; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: hm[i][j] = hmStar[i][j] = hasManaged[i][j]=='Y'; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<N; ++k) 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<N; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<N; ++j) 4fd800b3a8 2011-02-23 kinaba: hmStar[i][j] |= hmStar[i][k] & hmStar[k][j]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: graph g(N); 4fd800b3a8 2011-02-23 kinaba: int nextID = N; 4fd800b3a8 2011-02-23 kinaba: for(int p=0; p<N; ++p) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> cs; 4fd800b3a8 2011-02-23 kinaba: vector<int> pi; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int c=0; c<N; ++c) 4fd800b3a8 2011-02-23 kinaba: if( hm[p][c] && !hmStar[c][p] ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int pid = -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<cs.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( hmStar[c][cs[j]] && hmStar[cs[j]][c] ) 4fd800b3a8 2011-02-23 kinaba: {pid = pi[j]; break;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if(pid<0) pid = nextID++; 4fd800b3a8 2011-02-23 kinaba: cs.push_back(c); 4fd800b3a8 2011-02-23 kinaba: pi.push_back(pid); 4fd800b3a8 2011-02-23 kinaba: g[c].push_back(pid); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: g.resize(nextID); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return N - biMatch<5000>(g, N); 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() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string hasManaged_[] = {"NNNN","NNYN","NNNN","YYYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string hasManaged_[] = {"NNYN","NNYN","YNNN","YYYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string hasManaged_[] = {"NYNNN","NNYNN","NNNYN","NNNNY","YNNNN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 5; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string hasManaged_[] = {"NYNYYYNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNYYNN" 4fd800b3a8 2011-02-23 kinaba: ,"NYNNYYNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNYNYYNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"NYYNYNNN" 4fd800b3a8 2011-02-23 kinaba: ,"YYNYYYNN" 4fd800b3a8 2011-02-23 kinaba: ,"YYNYYYYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string hasManaged_[] = {"NYNYNNYYNYNNNYYNYNYY" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNNYYNNNYYNYNYNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNYYNNNYYNYNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"YNYNNNNYNNNYNNYNYNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NYNNNNNYYYYNYNYYNNYN" 4fd800b3a8 2011-02-23 kinaba: ,"YYYYNNYNYNNNNNYYNYNY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNNNNNNYYNNNNNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNYNNNNYYNYNNNYN" 4fd800b3a8 2011-02-23 kinaba: ,"NYYYNNNYNNNNYYNYYNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NNYNNNYYNNNNYNNNNNYY" 4fd800b3a8 2011-02-23 kinaba: ,"YYNNNYNNYNNNNYNNYNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNNYNNNNYNYNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNYNNNNYNNNNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNYNNNNNNNNYYNYNNYYN" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNNNNNNYNNNNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"YNYYNYYNNNYYNNNNYNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NYNNNNNYNYNYYYYNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NNYNNNNYNYNYYYNNNNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNNYNNNYNNNNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNYYNNNYYNYNNNYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string hasManaged_[] = {"NNNYNNNNNNNYNNNNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNYNNYNNNNNYNYYN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNNYNYNNNNNNNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNNNNNNNNNNNNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNYNNNYNYNNYNNYNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNYNNNNNNNYNNYYYNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NNYYNNNNYNNNNNNNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NYNNNYNNYNNNYNYNYNNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNNNNNNNNNNNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNNNNNNNYNNYYNNYN" 4fd800b3a8 2011-02-23 kinaba: ,"NNYYNYNNYYNNNNYYNYNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNNNNNYNNNNNNNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"NNNYYNYNYYYYNNNNYNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NNYYNYNNYYYYNNNNNYNY" 4fd800b3a8 2011-02-23 kinaba: ,"NNYYYNNNYNNNNNNNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNYYYNNNNNNNNYNYNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNYNYNNNNYNNYNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNYYYYNNYNNYNNNYNYN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNYYYYNYNNYNNYYYNNY" 4fd800b3a8 2011-02-23 kinaba: ,"YNYNNNNNYNNYNNNNNNNN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: string hasManaged_[] = {"NNNNN","NNNNN","NNNNN","NNNNN","NNNNN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 5; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: