Artifact Content
Not logged in

Artifact 0b6298dd57a02ed4b42048a7400253e5d16eb82f


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

typedef int           vert;
typedef vert          edge;
typedef vector<edge>  edges;
typedef vector<edges> graph;

bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] )
{
	for(int i=0; i<G[v].size(); ++i) {
		vert u = G[v][i];
		if( visited[u] ) continue;
		visited[u] = true;

		if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) )
			{ matchTo[v]=u, matchTo[u]=v; return true; }
	}
	return false;
}

template<int NV>
int biMatch( graph& G, int L ) // [0,L):left, [L,?):right
    // only left->right edges are used during computation
{
	vector<vert> matchTo(G.size(), -1);
	int ans = 0;
	for(vert v=0; v<L; ++v) {
		bool visited[NV] = {};
		if( augment(G, v, matchTo, visited) )
			++ans;
	}
	return ans;
}

class CompanyRestructuring {
public:
	int fewestDivisions(vector <string> hasManaged) 
	{
		int N = hasManaged.size();

		bool hm[50][50], hmStar[50][50];
		for(int i=0; i<N; ++i)
			for(int j=0; j<N; ++j)
				hm[i][j] = hmStar[i][j] = hasManaged[i][j]=='Y';

		for(int k=0; k<N; ++k)
			for(int i=0; i<N; ++i)
				for(int j=0; j<N; ++j)
					hmStar[i][j] |= hmStar[i][k] & hmStar[k][j];

		graph g(N);
		int nextID = N;
		for(int p=0; p<N; ++p)
		{
			vector<int> cs;
			vector<int> pi;

			for(int c=0; c<N; ++c)
				if( hm[p][c] && !hmStar[c][p] )
				{
					int pid = -1;
					
					for(int j=0; j<cs.size(); ++j)
						if( hmStar[c][cs[j]] && hmStar[cs[j]][c] )
							{pid = pi[j]; break;}

					if(pid<0) pid = nextID++;
					cs.push_back(c);
					pi.push_back(pid);
					g[c].push_back(pid);
				}
		}
		g.resize(nextID);

		return N - biMatch<5000>(g, 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> 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(); }
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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string hasManaged_[] = {"NNNN","NNYN","NNNN","YYYN"};
	  vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 
	int RetVal = 1; 
	return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); }
int Test_(Case_<1>) {
	string hasManaged_[] = {"NNYN","NNYN","YNNN","YYYN"};
	  vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 
	int RetVal = 1; 
	return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); }
int Test_(Case_<2>) {
	string hasManaged_[] = {"NYNNN","NNYNN","NNNYN","NNNNY","YNNNN"};
	  vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 
	int RetVal = 5; 
	return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); }
int Test_(Case_<3>) {
	string hasManaged_[] = {"NYNYYYNN"
,"NNNNYYNN"
,"NYNNYYNN"
,"NNYNYYNN"
,"NNNNNNNN"
,"NYYNYNNN"
,"YYNYYYNN"
,"YYNYYYYN"};
	  vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 
	int RetVal = 1; 
	return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); }
int Test_(Case_<4>) {
	string hasManaged_[] = {"NYNYNNYYNYNNNYYNYNYY"
,"YNNNNNYYNNNYYNYNYNYY"
,"NNNNNNYYNNNYYNYNNNNY"
,"YNYNNNNYNNNYNNYNYNNY"
,"NYNNNNNYYYYNYNYYNNYN"
,"YYYYNNYNYNNNNNYYNYNY"
,"NNNNNNNNNNNYYNNNNNYY"
,"NNNNNNYNNNNYYNYNNNYN"
,"NYYYNNNYNNNNYYNYYNYY"
,"NNYNNNYYNNNNYNNNNNYY"
,"YYNNNYNNYNNNNYNNYNYY"
,"NNNNNNNYNNNNYNYNNNNY"
,"NNNNNNYNNNNYNNNNNNNN"
,"NNYNNNNNNNNYYNYNNYYN"
,"NNNNNNNNNNNYNNNNNNNY"
,"YNYYNYYNNNYYNNNNYNYY"
,"NYNNNNNYNYNYYYYNNNNY"
,"NNYNNNNYNYNYYYNNNNYY"
,"NNNNNNNYNNNYNNNNNNNY"
,"NNNNNNYYNNNYYNYNNNYN"};
	  vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 
	int RetVal = 4; 
	return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); }
int Test_(Case_<5>) {
	string hasManaged_[] = {"NNNYNNNNNNNYNNNNNNNN"
,"NNNNNNYNNYNNNNNYNYYN"
,"YNNNNNYNYNNNNNNNNNNY"
,"YNNNNNNNNNNNNNNNNNNN"
,"NNYNNNYNYNNYNNYNNNNY"
,"NNNYNNNNNNNYNNYYYNYY"
,"NNYYNNNNYNNNNNNNNNNY"
,"NYNNNYNNYNNNYNYNYNNN"
,"NNNNNNNNNNNNNNNNNNNN"
,"YNNNNNNNNNNYNNYYNNYN"
,"NNYYNYNNYYNNNNYYNYNN"
,"NNNNNNNNYNNNNNNNNNNN"
,"NNNYYNYNYYYYNNNNYNYY"
,"NNYYNYNNYYYYNNNNNYNY"
,"NNYYYNNNYNNNNNNNNNNN"
,"YNNYYYNNNNNNNNYNYNYY"
,"NNNNYNYNNNNYNNYNNNNN"
,"YNNYYYYNNYNNYNNNYNYN"
,"YNNYYYYNYNNYNNYYYNNY"
,"YNYNNNNNYNNYNNNNNNNN"};
	  vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 
	int RetVal = 2; 
	return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); }
int Test_(Case_<6>) {
	string hasManaged_[] = {"NNNNN","NNNNN","NNNNN","NNNNN","NNNNN"};
	  vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_)/sizeof(*hasManaged_)); 
	int RetVal = 5; 
	return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasManaged)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE