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: 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: static const int NV = 100; 4fd800b3a8 2011-02-23 kinaba: int biMatch( graph& G, int L ) // [0,L):left, [L,?):right 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 PeopleYouMayKnow { public: 4fd800b3a8 2011-02-23 kinaba: int maximalScore(vector <string> friends, int personA, int personB) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int removed = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // A-C-B : C must be removed 4fd800b3a8 2011-02-23 kinaba: int N = friends.size(); 4fd800b3a8 2011-02-23 kinaba: int A = personA, B = personB; 4fd800b3a8 2011-02-23 kinaba: for(int C=0; C<N; ++C) if(C!=A && C!=B) 4fd800b3a8 2011-02-23 kinaba: if( friends[A][C]=='Y' && friends[C][B]=='Y' ) { 4fd800b3a8 2011-02-23 kinaba: removed ++; 4fd800b3a8 2011-02-23 kinaba: for(int D=0; D<N; ++D) 4fd800b3a8 2011-02-23 kinaba: friends[C][D] = friends[D][C] = 'N'; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // A-A1, B1-B : A1-B1 must be disconnected 4fd800b3a8 2011-02-23 kinaba: vector<int> A1; 4fd800b3a8 2011-02-23 kinaba: for(int C=0; C<N; ++C) if(C!=A && C!=B) 4fd800b3a8 2011-02-23 kinaba: if( friends[A][C]=='Y' ) 4fd800b3a8 2011-02-23 kinaba: A1.push_back(C); 4fd800b3a8 2011-02-23 kinaba: vector<int> B1; 4fd800b3a8 2011-02-23 kinaba: for(int C=0; C<N; ++C) if(C!=A && C!=B) 4fd800b3a8 2011-02-23 kinaba: if( friends[C][B]=='Y' ) 4fd800b3a8 2011-02-23 kinaba: B1.push_back(C); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // == bimatch 4fd800b3a8 2011-02-23 kinaba: graph G(A1.size()+B1.size()); 4fd800b3a8 2011-02-23 kinaba: for(int a=0; a<A1.size(); ++a) 4fd800b3a8 2011-02-23 kinaba: for(int b=0; b<B1.size(); ++b) 4fd800b3a8 2011-02-23 kinaba: if( friends[A1[a]][B1[b]]=='Y' ) 4fd800b3a8 2011-02-23 kinaba: G[a].push_back( A1.size()+b ); 4fd800b3a8 2011-02-23 kinaba: return removed + biMatch(G, A1.size()); 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 int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 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(_, PeopleYouMayKnow().maximalScore(friends, personA, personB));} 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 friends_[] = {"NN" 4fd800b3a8 2011-02-23 kinaba: ,"NN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 4fd800b3a8 2011-02-23 kinaba: int personA = 0; 4fd800b3a8 2011-02-23 kinaba: int personB = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string friends_[] = {"NYNN" 4fd800b3a8 2011-02-23 kinaba: ,"YNYN" 4fd800b3a8 2011-02-23 kinaba: ,"NYNY" 4fd800b3a8 2011-02-23 kinaba: ,"NNYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 4fd800b3a8 2011-02-23 kinaba: int personA = 0; 4fd800b3a8 2011-02-23 kinaba: int personB = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string friends_[] = {"NYNYYYN" 4fd800b3a8 2011-02-23 kinaba: ,"YNYNNYY" 4fd800b3a8 2011-02-23 kinaba: ,"NYNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNYN" 4fd800b3a8 2011-02-23 kinaba: ,"YYNNYNY" 4fd800b3a8 2011-02-23 kinaba: ,"NYYNNYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 4fd800b3a8 2011-02-23 kinaba: int personA = 2; 4fd800b3a8 2011-02-23 kinaba: int personB = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string friends_[] = {"NYYYYNNNN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNYYYN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNNNYN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNNNYN" 4fd800b3a8 2011-02-23 kinaba: ,"YNNNNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NYNNNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NYNNNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NYYYNNNNY" 4fd800b3a8 2011-02-23 kinaba: ,"NNNNYYYYN"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 4fd800b3a8 2011-02-23 kinaba: int personA = 8; 4fd800b3a8 2011-02-23 kinaba: int personB = 0; 4fd800b3a8 2011-02-23 kinaba: int _ = 3; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE