Artifact Content
Not logged in

Artifact c3aca7366b0837d8e80e1752458133ac5bc5c5bb


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <memory>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

template<typename T>
class IdGen
{
	map<T, int> v2id_;
	vector<T>   id2v_;
public:
	int v2id(const T& v) {
		if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); }
		return v2id_[v];
	}
	const T& id2v(int i) const { return id2v_[i]; }
	int size() const { return id2v_.size(); }
};

template<typename Vert, typename Flow, int NV=512>
class MaxFlow
{
	IdGen<Vert> idgen;
	vector<int> G[NV];
	Flow F[NV][NV];

public:
	void addEdge( Vert s_, Vert t_, Flow f )
	{
		const int s = idgen.v2id(s_), t = idgen.v2id(t_);
		G[s].push_back(t);
		G[t].push_back(s);
		F[s][t] = f;
		F[t][s] = 0;
	}

	Flow calc( Vert s_, Vert t_ )
	{
		const int S = idgen.v2id(s_), D = idgen.v2id(t_);
		for( Flow total=0 ;; ) {
			// Do BFS and compute the level for each node.
			int LV[NV] = {0};
			vector<int> Q(1, S);
			for(int lv=1; !Q.empty(); ++lv) {
				vector<int> Q2;
				for(size_t i=0; i!=Q.size(); ++i) {
					const vector<int>& ne = G[Q[i]];
					for(size_t j=0; j!=ne.size(); ++j)
						if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j]!=S )
							LV[ne[j]]=lv, Q2.push_back(ne[j]);
				}
				Q.swap(Q2);
			}

			// Destination is now unreachable. Done.
			if( !LV[D] )
				return total;

			// Iterating DFS.
			bool blocked[NV] = {};
			total += dinic_dfs( S, D, LV, 0x7fffffff, blocked );
		}
	}

private:
	Flow dinic_dfs( int v, int D, int LV[], Flow flow_in, bool blocked[] )
	{
		Flow flow_out = 0;
		for(size_t i=0; i!=G[v].size(); ++i) {
			int u = G[v][i];
			if( LV[v]+1==LV[u] && F[v][u] ) {
				Flow f = min(flow_in-flow_out, F[v][u]);
				if( u==D || !blocked[u] && (f=dinic_dfs(u,D,LV,f,blocked))>0 ) {
					F[v][u]  -= f;
					F[u][v]  += f;
					flow_out += f;
					if( flow_in == flow_out ) return flow_out;
				}
			}
		}
		blocked[v] = (flow_out==0);
		return flow_out;
	}
};

class DancingParty { public:
	int maxDances(vector <string> likes, int k)
	{
		const int n = likes.size();
		for(int M=1; M<=n; ++M) {
			typedef pair<int,int> Vert;
			auto_ptr< MaxFlow<Vert, int> > mf(new MaxFlow<Vert, int>);

			for(int i=0; i<n; ++i) {
				mf->addEdge(Vert(0,0), Vert(1,i), M); // src L
				mf->addEdge(Vert(1,i), Vert(2,i), M); // L YL
				mf->addEdge(Vert(1,i), Vert(3,i), k); // L NL
				mf->addEdge(Vert(4,i), Vert(6,i), M); // YR R
				mf->addEdge(Vert(5,i), Vert(6,i), k); // NR R
				mf->addEdge(Vert(6,i), Vert(7,0), M); // R dst

				for(int j=0; j<n; ++j)
					if( likes[i][j] == 'Y' )
						mf->addEdge(Vert(2,i), Vert(4,j), 1);
					else
						mf->addEdge(Vert(3,i), Vert(5,j), 1);
			}

			if( mf->calc(Vert(0,0), Vert(7,0)) < M*n )
				return M-1;
		}
		return n;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, DancingParty().maxDances(likes, k));}
int main(){

CASE(0)
	string likes_[] = {"YYY", "YYY", "YYY"};
	  vector <string> likes(likes_, likes_+sizeof(likes_)/sizeof(*likes_)); 
	int k = 0; 
	int _ = 3; 
END
CASE(1)
	string likes_[] = {"YYY", "YYN", "YNY"};
	  vector <string> likes(likes_, likes_+sizeof(likes_)/sizeof(*likes_)); 
	int k = 0; 
	int _ = 2; 
END
CASE(2)
	string likes_[] = {"YN", "YN"};
	  vector <string> likes(likes_, likes_+sizeof(likes_)/sizeof(*likes_)); 
	int k = 0; 
	int _ = 0; 
END
CASE(3)
	string likes_[] = {"YN", "YN"};
	  vector <string> likes(likes_, likes_+sizeof(likes_)/sizeof(*likes_)); 
	int k = 1; 
	int _ = 1; 
END
/*
CASE(4)
	string likes_[] = ;
	  vector <string> likes(likes_, likes_+sizeof(likes_)/sizeof(*likes_)); 
	int k = ; 
	int _ = ; 
END
CASE(5)
	string likes_[] = ;
	  vector <string> likes(likes_, likes_+sizeof(likes_)/sizeof(*likes_)); 
	int k = ; 
	int _ = ; 
END
*/
}
// END CUT HERE