Artifact Content
Not logged in

Artifact a664e23344c8df166c8f0c9eb909dd289b030b89


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class ChromaticNumber { public:
	int minColors(vector <string> graph)
	{
		const int N = graph.size();

		vector< vector<bool> > conn(N, vector<bool>(N, false));
		vector<int> deg(N);
		for(int i=0; i<N; ++i)
		for(int j=0; j<N; ++j)
			if(i!=j) {
				conn[i][j] = (graph[i][j]=='N');
				deg[i] += (graph[i][j]=='N');
			}

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

		int answer = 0;

		vector<bool> used(N, false);
		for(int i=0; i<N; ++i) if(!used[i]) {
			vector<int> ps(1, i);
			for(int k=i+1; k<N; ++k)
				if( conn[i][k] )
					ps.push_back(k);
			bool all_deg2 = true;
			for(int k=0; k<ps.size(); ++k) {
				used[ps[k]] = true;
				all_deg2 = all_deg2 && (deg[ps[k]]==2);
			}
			answer += (all_deg2 ? cycle(ps.size()) : line(ps.size()));
		}

		return answer;
	}

	int cycle(int N)
	{
		return N==3 ? 1 : (N+1)/2;
	}

	int line(int N)
	{
		return (N+1)/2;
	}
};

// 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(_, ChromaticNumber().minColors(graph));}
int main(){

CASE(0)
	string graph_[] = {"N"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 1; 
END
CASE(1)
	string graph_[] = {"NYY",
 "YNN",
 "YNN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 2; 
END
CASE(2)
	string graph_[] = {"NYNN",
 "YNNN",
 "NNNY",
 "NNYN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 2; 
END
CASE(3)
	string graph_[] = {"NYNY",
 "YNYY",
 "NYNN",
 "YYNN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 3; 
END
CASE(4)
	string graph_[] = {"NYYYYYYY",
 "YNYYYYYY",
 "YYNYYYYY",
 "YYYNYYYY",
 "YYYYNYYY",
 "YYYYYNYY",
 "YYYYYYNY",
 "YYYYYYYN"};
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = 8; 
END
/*
CASE(5)
	string graph_[] = ;
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = ; 
END
CASE(6)
	string graph_[] = ;
	  vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); 
	int _ = ; 
END
*/
}
// END CUT HERE