Artifact Content
Not logged in

Artifact 7c1aa38966342ddc045ac9b0d368a24c39db0827


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

double C(int n, int k)
{
	k = min(n-k, k);
	double t = 1;
	for(int i=1; i<=k; ++i)
		t = t * (n-i+1) / i;
	return t;
}

class EagleInZoo { public:
	double calc(vector <int> parent, int K)
	{
		int N = parent.size() + 1;
		vector<vector<int>> child(N);
		for(int i=1; i<N; ++i)
			child[parent[i-1]].push_back(i);
		return rec(child, 0, K);
	}

	map<pair<int,int>,double> memo;
	double rec(const vector<vector<int>>& child, int v, int K)
	{
		if(K == 1)
			return 1.0;
		if(child[v].empty())
			return 0.0;
		pair<int,int> key(v, K);
		if(memo.count(key)) return memo[key];

		double ans = 0.0;
		for(int u: child[v]) // When K-the eagle goes to |u|.
			for(int uN=0; uN<=K-2; ++uN) // When uN out of K-2 previous eagles went to |u|.
			{
				ans += C(K-2, uN)
					* pow(1.0/child[v].size(), uN)
					* pow(1.0-1.0/child[v].size(), K-2-uN)
					* rec(child, u, uN+1);
			}
		return memo[key] = ans / child[v].size();
	}
};

// 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(_, EagleInZoo().calc(parent, K));}
int main(){

CASE(0)
	int parent_[] = {0,0};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int K = 2; 
	double _ = 1.0; 
END
CASE(1)
	int parent_[] = {0,0};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int K = 3; 
	double _ = 0.5; 
END
CASE(2)
	int parent_[] = {0,1,0,3};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int K = 4; 
	double _ = 0.75; 
END
CASE(3)
	int parent_[] = {0,0,1,1,2,4,4,4,5,5,6,6};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int K = 20; 
	double _ = 0.14595168754091617; 
END
CASE(4)
	int parent_[] = {0,1,2,3,2,1,1,7,0,9,10,11,12,13,14,15,16,17,18,14,9};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int K = 24; 
	double _ = 0.3272154791654077; 
END
CASE(5)
	int parent_[] = {0,1,2,3,4,5,6,7,8,9,10};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int K = 50; 
	double _ = 0.0; 
END
/*
CASE(6)
	int parent_[] = ;
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int K = ; 
	double _ = ; 
END
CASE(7)
	int parent_[] = ;
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int K = ; 
	double _ = ; 
END
*/
}
// END CUT HERE