be86f38991 2013-09-07 kinaba: #include <iostream> be86f38991 2013-09-07 kinaba: #include <sstream> be86f38991 2013-09-07 kinaba: #include <iomanip> be86f38991 2013-09-07 kinaba: #include <vector> be86f38991 2013-09-07 kinaba: #include <string> be86f38991 2013-09-07 kinaba: #include <map> be86f38991 2013-09-07 kinaba: #include <set> be86f38991 2013-09-07 kinaba: #include <algorithm> be86f38991 2013-09-07 kinaba: #include <numeric> be86f38991 2013-09-07 kinaba: #include <iterator> be86f38991 2013-09-07 kinaba: #include <functional> be86f38991 2013-09-07 kinaba: #include <complex> be86f38991 2013-09-07 kinaba: #include <queue> be86f38991 2013-09-07 kinaba: #include <stack> be86f38991 2013-09-07 kinaba: #include <cmath> be86f38991 2013-09-07 kinaba: #include <cassert> be86f38991 2013-09-07 kinaba: #include <tuple> be86f38991 2013-09-07 kinaba: using namespace std; be86f38991 2013-09-07 kinaba: typedef long long LL; be86f38991 2013-09-07 kinaba: typedef complex<double> CMP; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: typedef int vert; be86f38991 2013-09-07 kinaba: typedef vert edge; be86f38991 2013-09-07 kinaba: typedef vector<edge> edges; be86f38991 2013-09-07 kinaba: typedef vector<edges> graph; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: for(int i=0; i<G[v].size(); ++i) { be86f38991 2013-09-07 kinaba: vert u = G[v][i]; be86f38991 2013-09-07 kinaba: if( visited[u] ) continue; be86f38991 2013-09-07 kinaba: visited[u] = true; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) be86f38991 2013-09-07 kinaba: { matchTo[v]=u, matchTo[u]=v; return true; } be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: return false; be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: template<int NV> be86f38991 2013-09-07 kinaba: int biMatch( graph& G, int L ) // [0,L):left, [L,?):right be86f38991 2013-09-07 kinaba: // only left->right edges are used during computation be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: vector<vert> matchTo(G.size(), -1); be86f38991 2013-09-07 kinaba: int ans = 0; be86f38991 2013-09-07 kinaba: for(vert v=0; v<L; ++v) { be86f38991 2013-09-07 kinaba: bool visited[NV] = {}; be86f38991 2013-09-07 kinaba: if( augment(G, v, matchTo, visited) ) be86f38991 2013-09-07 kinaba: ++ans; be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: return ans; be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: class GearsDiv1 { public: be86f38991 2013-09-07 kinaba: int getmin(string color, vector <string> graph) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: vector<int> r, g, b; be86f38991 2013-09-07 kinaba: for(int i=0; i<color.size(); ++i) be86f38991 2013-09-07 kinaba: (color[i]=='R' ? r : color[i]=='G' ? g : b).push_back(i); be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: int rg = calc(graph, r, g); be86f38991 2013-09-07 kinaba: int gb = calc(graph, g, b); be86f38991 2013-09-07 kinaba: int br = calc(graph, b, r); be86f38991 2013-09-07 kinaba: return min(rg, min(gb, br)); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: int calc(const vector<string>& G, const vector<int>& A, const vector<int>& B) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: if(A.empty() || B.empty()) be86f38991 2013-09-07 kinaba: return 0; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: graph gr(A.size() + B.size()); be86f38991 2013-09-07 kinaba: for(int a=0; a<A.size(); ++a) be86f38991 2013-09-07 kinaba: for(int b=0; b<B.size(); ++b) be86f38991 2013-09-07 kinaba: { be86f38991 2013-09-07 kinaba: if(G[A[a]][B[b]] == 'Y') be86f38991 2013-09-07 kinaba: gr[a].push_back(A.size()+b); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: return biMatch<256>(gr, A.size()); be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: }; be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: // BEGIN CUT HERE be86f38991 2013-09-07 kinaba: #include <ctime> be86f38991 2013-09-07 kinaba: double start_time; string timer() be86f38991 2013-09-07 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } be86f38991 2013-09-07 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) be86f38991 2013-09-07 kinaba: { os << "{ "; be86f38991 2013-09-07 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) be86f38991 2013-09-07 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } be86f38991 2013-09-07 kinaba: void verify_case(const int& Expected, const int& Received) { be86f38991 2013-09-07 kinaba: bool ok = (Expected == Received); be86f38991 2013-09-07 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; be86f38991 2013-09-07 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } be86f38991 2013-09-07 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); be86f38991 2013-09-07 kinaba: #define END verify_case(_, GearsDiv1().getmin(color, graph));} be86f38991 2013-09-07 kinaba: int main(){ be86f38991 2013-09-07 kinaba: be86f38991 2013-09-07 kinaba: CASE(0) be86f38991 2013-09-07 kinaba: string color = "RGB"; be86f38991 2013-09-07 kinaba: string graph_[] = {"NYY","YNY","YYN"}; be86f38991 2013-09-07 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); be86f38991 2013-09-07 kinaba: int _ = 1; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(1) be86f38991 2013-09-07 kinaba: string color = "RGBR"; be86f38991 2013-09-07 kinaba: string graph_[] = {"NNNN","NNNN","NNNN","NNNN"}; be86f38991 2013-09-07 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); be86f38991 2013-09-07 kinaba: int _ = 0; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(2) be86f38991 2013-09-07 kinaba: string color = "RGBR"; be86f38991 2013-09-07 kinaba: string graph_[] = {"NYNN","YNYN","NYNY","NNYN"}; be86f38991 2013-09-07 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); be86f38991 2013-09-07 kinaba: int _ = 1; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(3) be86f38991 2013-09-07 kinaba: string color = "RRRRRGRRBGRRGBBGGGBRRRGBRGRRGG"; be86f38991 2013-09-07 kinaba: string graph_[] = {"NNNNNYNNNYNNYNNNYNNNNNNNNYNNYY", be86f38991 2013-09-07 kinaba: "NNNNNNNNYNNNYNYNNYNNNNYNNYNNYY", be86f38991 2013-09-07 kinaba: "NNNNNYNNNNNNNNNNNNYNNNNNNYNNNY", be86f38991 2013-09-07 kinaba: "NNNNNNNNNYNNYNNYYYNNNNYNNYNNNN", be86f38991 2013-09-07 kinaba: "NNNNNNNNNYNNYNNYYYNNNNYNNNNNNN", be86f38991 2013-09-07 kinaba: "YNYNNNYYYNNYNYYNNNNNYYNYNNYYNN", be86f38991 2013-09-07 kinaba: "NNNNNYNNNNNNNNNYYYNNNNYNNYNNYY", be86f38991 2013-09-07 kinaba: "NNNNNYNNNNNNNNNYNNNNNNNNNNNNYN", be86f38991 2013-09-07 kinaba: "NYNNNYNNNYNNYNNYYYNNNNYNNYNNYY", be86f38991 2013-09-07 kinaba: "YNNYYNNNYNNNNYYNNNYNYYNYNNNNNN", be86f38991 2013-09-07 kinaba: "NNNNNNNNNNNNYNNYNYNNNNYNNNNNNY", be86f38991 2013-09-07 kinaba: "NNNNNYNNNNNNYNNYYYNNNNNNNNNNYN", be86f38991 2013-09-07 kinaba: "YYNYYNNNYNYYNYYNNNYNYNNYNNNNNN", be86f38991 2013-09-07 kinaba: "NNNNNYNNNYNNYNNYYYNNNNYNNYNYYY", be86f38991 2013-09-07 kinaba: "NYNNNYNNNYNNYNNYYYNNNNYNNYNNYY", be86f38991 2013-09-07 kinaba: "NNNYYNYYYNYYNYYNNNYNYNNYYNYYNN", be86f38991 2013-09-07 kinaba: "YNNYYNYNYNNYNYYNNNYNNNNYYNNYNN", be86f38991 2013-09-07 kinaba: "NYNYYNYNYNYYNYYNNNNYYNNYYNYNNN", be86f38991 2013-09-07 kinaba: "NNYNNNNNNYNNYNNYYNNNNNYNNYNNNY", be86f38991 2013-09-07 kinaba: "NNNNNNNNNNNNNNNNNYNNNNYNNYNNNY", be86f38991 2013-09-07 kinaba: "NNNNNYNNNYNNYNNYNYNNNNYNNNNNYY", be86f38991 2013-09-07 kinaba: "NNNNNYNNNYNNNNNNNNNNNNYNNNNNNN", be86f38991 2013-09-07 kinaba: "NYNYYNYNYNYNNYYNNNYYYYNYYNYNNN", be86f38991 2013-09-07 kinaba: "NNNNNYNNNYNNYNNYYYNNNNYNNNNNNY", be86f38991 2013-09-07 kinaba: "NNNNNNNNNNNNNNNYYYNNNNYNNYNNYY", be86f38991 2013-09-07 kinaba: "YYYYNNYNYNNNNYYNNNYYNNNNYNYYNN", be86f38991 2013-09-07 kinaba: "NNNNNYNNNNNNNNNYNYNNNNYNNYNNYN", be86f38991 2013-09-07 kinaba: "NNNNNYNNNNNNNYNYYNNNNNNNNYNNYY", be86f38991 2013-09-07 kinaba: "YYNNNNYYYNNYNYYNNNNNYNNNYNYYNN", be86f38991 2013-09-07 kinaba: "YYYNNNYNYNYNNYYNNNYYYNNYYNNYNN"}; be86f38991 2013-09-07 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); be86f38991 2013-09-07 kinaba: int _ = 3; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: /* be86f38991 2013-09-07 kinaba: CASE(4) be86f38991 2013-09-07 kinaba: string color = ; be86f38991 2013-09-07 kinaba: string graph_[] = ; be86f38991 2013-09-07 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); be86f38991 2013-09-07 kinaba: int _ = ; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: CASE(5) be86f38991 2013-09-07 kinaba: string color = ; be86f38991 2013-09-07 kinaba: string graph_[] = ; be86f38991 2013-09-07 kinaba: vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); be86f38991 2013-09-07 kinaba: int _ = ; be86f38991 2013-09-07 kinaba: END be86f38991 2013-09-07 kinaba: */ be86f38991 2013-09-07 kinaba: } be86f38991 2013-09-07 kinaba: // END CUT HERE