Artifact Content
Not logged in

Artifact 98c2d671691b0a489d3306b1a7ab8557e6d2ed0e


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

template<typename T>
struct DP2
{
	int N1, N2;
	vector<T> data;
	DP2(){}
	DP2(int N1, int N2, const T& t = T())
		: N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2)
		{ return data[ (i1*N2)+i2 ]; }
	void swap(DP2& rhs)
		{ data.swap(rhs.data); }
};

class FriendTour { public:
	double tourProbability(vector <string> friends, int K) 
	{
		int N = (int)friends.size();
		vector< vector<int> > f(N);
		for(int i=0; i<N; ++i)
		{
			stringstream sin( friends[i] );
			for(int v; sin>>v; )
				f[i].push_back(v-1);
		}

		map<int,int> rename;
		rename[0] = 0;
		for(int k=0; k<f[0].size(); ++k)
		{
			int newid = rename.size();
			rename[f[0][k]] = newid;
		}

		vector< vector<int> > ff(rename.size());
		for(int i=0; i<N; ++i)
			if( rename.count(i) )
			{
				int k = rename[i];
				for(int j=0; j<f[i].size(); ++j)
					if( f[i][j]!=0 && rename.count(f[i][j]) )
						ff[k].push_back( rename[f[i][j]] );
					else
						ff[k].push_back( -1 );
			}
		return solve(ff, K);
	}

	double solve(vector<vector<int> >& f, int K)
	{
		memo = DP2<double>(f.size(), 1<<f.size(), -1);
		return rec(f, K, 0, 1);
	}

	double C(int N, int K)
	{
		double p = 1.0;
		for(int i=0; i<min(K,N-K); ++i)
			p = p * (N-i) / (1+i);
		return p;
	}

	DP2<double> memo;
	double rec(vector<vector<int> >& f, int K, int v, int mask)
	{
		if( memo(v,mask) != -1 )
			return memo(v,mask);

		if( mask == (1<<f.size())-1 )
			return 1.0;

		vector<double> nxt;
		for(int i=0; i<f[v].size(); ++i)
			if( f[v][i]==-1 || (mask & (1<<f[v][i])) )
				nxt.push_back(0);
			else
				nxt.push_back( rec(f,K,f[v][i],mask|(1<<f[v][i])) );
		sort(nxt.rbegin(), nxt.rend());
		int N = nxt.size();
		if( N == 0 )
			return memo(v,mask) = 0.0;

		if( N <= K )
			return memo(v,mask) = nxt[0];

		double p = 0.0;
		for(int i=0; i<nxt.size(); ++i) {
			if( N-1-i < K ) {
				p += C(N-i,K) / C(N,K) * nxt[i];
				break;
			}
			p += (C(N-i,K) - C(N-1-i,K)) / C(N,K) * nxt[i];
		}
		return memo(v,mask) = p;
	}
};

// 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(_, FriendTour().tourProbability(friends, K));}
int main(){

CASE(0)
	string friends_[] = {"2 3 4",
 "1 3 4",
 "1 2 4",
 "1 2 3"};
	  vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 
	int K = 1; 
	double _ = 0.2222222222222222; 
END
CASE(1)
	string friends_[] = {"2 3 4",
 "1 3 4",
 "1 2 4",
 "1 2 3"};
	  vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 
	int K = 2; 
	double _ = 0.6666666666666666; 
END
CASE(2)
	string friends_[] = {"3 2 4",
 "3 5 1",
 "5 2 1 4",
 "3 1 5",
 "3 2 4"};
	  vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 
	int K = 2; 
	double _ = 0.3333333333333333; 
END
CASE(3)
	string friends_[] = {"3 2 4",
 "3 5 1",
 "5 2 1 4",
 "3 1 5 6",
 "3 2 4",
 "4"}
;
	  vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 
	int K = 2; 
	double _ = 0.3055555555555556; 
END
CASE(4)
	string friends_[] = {"6 5 4 2",
 "1 6 3 5",
 "5 4 2",
 "3 1 5",
 "2 4 3 1 6",
 "1 2 5"};
	  vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 
	int K = 3; 
	double _ = 0.73125; 
END
CASE(5)
	string friends_[] = {"2 3 4 5 6 7 8 9 10 11 12 13 14 15 16", "1 3 4 5 6 7 8 9 10 11 12 13 14 15 16", "1 2 4 5 6 7 8 9 10 11 12 13 14 15 16", "1 2 3 5 6 7 8 9 10 11 12 13 14 15 16", "1 2 3 4 6 7 8 9 10 11 12 13 14 15 16", "1 2 3 4 5 7 8 9 10 11 12 13 14 15 16", "1 2 3 4 5 6 8 9 10 11 12 13 14 15 16", "1 2 3 4 5 6 7 9 10 11 12 13 14 15 16", "1 2 3 4 5 6 7 8 10 11 12 13 14 15 16", "1 2 3 4 5 6 7 8 9 11 12 13 14 15 16", "1 2 3 4 5 6 7 8 9 10 12 13 14 15 16", "1 2 3 4 5 6 7 8 9 10 11 13 14 15 16", "1 2 3 4 5 6 7 8 9 10 11 12 14 15 16", "1 2 3 4 5 6 7 8 9 10 11 12 13 15 16", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 16", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"};
	  vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*friends_)); 
	int K = 3; 
	double _ = 0.010989546288518458; 
END
}
// END CUT HERE