Artifact Content
Not logged in

Artifact adf3912f0e556cd5ed2d7688d4d54e5c9accb919


#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;

class BunnyExam { public:
	double getExpected(int m, int k, vector <int> linkage) 
	{
		// trivially impossible cases
		if( k==1 && m>1	)
			return -1;
		if( k==2 )
			for(int i=0; i<linkage.size(); i+=2)
				if( (linkage[i] ^ linkage[i+1])&1 )
					return -1;
		for(int i=0; i<linkage.size(); i+=2)
			if( abs(linkage[i] - linkage[i+1]) == 1 )
				return -1;

		// interference graph
		int M = linkage.size()/2;
		vector< vector<int> > conflict(M);
		for(int i=0; i<M; ++i)
		for(int j=0; j<M; ++j)
			if( abs(linkage[2*i+0]-linkage[2*j+0])==1 || abs(linkage[2*i+1]-linkage[2*j+0])==1
			 || abs(linkage[2*i+0]-linkage[2*j+1])==1 || abs(linkage[2*i+1]-linkage[2*j+1])==1 )
				conflict[i].push_back(j);

		// check k-colorability
		if( colorable(conflict, k) )
			return double(m)/k;
		else
			return -1;
	}

	bool colorable(vector< vector<int> >& G, int k)
	{
		if( G.size() <= k )
			return true;
		vector<int> color(G.size(), (1<<k)-1);
		color[0] = 1;
		return rec(G, color, 0);
	}

	bool rec(vector< vector<int> >& G, const vector<int>& color, int i)
	{
		if( i == color.size() )
			return true;
		for(int k=1; k<=color[i]; k<<=1)
			if( color[i] & k ) // use color k
			{
				vector<int> c2 = color;
				for(int j=0; j<G[i].size(); ++j) // G[i][j] cannot use k
					c2[G[i][j]] &= ~k;
				if( rec(G, c2, i+1) )
					return true;
			}
		return false;
	}
};

// 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 double& Expected, const double& Received) {
 bool ok = (abs(Expected - Received) < 1e-9);
 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(_, BunnyExam().getExpected(m, k, linkage));}
int main(){

CASE(0)
	int m = 3; 
	int k = 2; 
	int linkage_[] = { 1, 3 };
	  vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 
	double _ = 1.5; 
END
CASE(1)
	int m = 4; 
	int k = 2; 
	int linkage_[] = { 1, 4 };
	  vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 
	double _ = -1.0; 
END
CASE(2)
	int m = 2; 
	int k = 8; 
	vector <int> linkage; 
	double _ = 0.25; 
END
CASE(3)
	int m = 1000000000; 
	int k = 1; 
	int linkage_[] = { 11, 13, 2010, 487 };
	  vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 
	double _ = -1.0; 
END
CASE(4)
	int m = 128; 
	int k = 64; 
	int linkage_[] = { 32, 16, 8, 4, 2, 1 }
;
	  vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 
	double _ = -1.0; 
END
CASE(5)
	int m = 13; 
	int k = 3; 
	int linkage_[] = { 1, 3, 7, 9, 13, 10, 6, 2  }
;
	  vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 
	double _ = 4.333333333333333; 
END
CASE(6)
	int m = 8; 
	int k = 3; 
	int linkage_[] = {1,4,2,6,3,8,5,7};
	  vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 
	double _ = -1; 
END
CASE(7)
	int m = 1000000000; 
	int k = 4; 
	int linkage_[] = {1,4,2,6,3,8,5,7,9,19,  10,20,11,21,12,22,13,23,14,24};
	  vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 
	double _ = -1; 
END
CASE(8)
	int m = 20; 
	int k = 3; 
	int linkage_[] = {19, 6, 1, 14, 13, 9, 20, 10, 15, 8, 17, 5, 12, 7, 16, 2, 18, 4, 3, 11};
	  vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linkage_)); 
	double _ = 6.666666666666667; 
END
}
// END CUT HERE