Artifact Content
Not logged in

Artifact cfe4f511e056164f5955cfb6376c7da16e83dba2


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

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N) : uf(N), sz(N, 1), nc(N)
	{
		for (int i = 0; i<N; ++i) uf[i] = i;
	}
	int size()
	{
		return nc;
	}
	int size(int a)
	{
		return sz[Find(a)];
	}
	int Find(int a)
	{
		return uf[a] == a ? a : uf[a] = Find(uf[a]);
	}
	// Semi-compression w.o. recursion.
	//{ while(uf[a] != a) a=uf[a]=uf[uf[a]]; return a; }
	bool Union(int a, int b)
	{
		a = Find(a);
		b = Find(b);
		if (a != b)
		{
			if (sz[a] >= sz[b]) swap(a, b);
			uf[a] = b;
			sz[b] += sz[a];
			--nc;
		}
		return (a != b);
	}
};

static const unsigned MODVAL = 1000000007;

class DengklekGaneshAndTree { public:
	int getCount(string labels, vector <int> parents)
	{
		const int N = labels.size();

		vector<vector<int>> children(N);
		UnionFind uf(N);
		for (int i = 1; i < N; ++i) {
			int u = parents[i-1], v = i;
			children[u].push_back(v);
			if (labels[u] == labels[v])
				uf.Union(u, v);
		}

		vector<int> level(N);
		{
			function<void(int, int)> rec; rec = [&](int v, int lv) {
				level[v] = lv;
				for (int u : children[v])
					rec(u, lv + 1);
			};
			rec(0, 0);
		}

		map<int, int> lb, ub;
		for (int v = 0; v < N; ++v) {
			int r = uf.Find(v), lv = level[v];
			if (lb.count(r) == 0) {
				lb[r] = lv;
				ub[r] = lv;
			}
			else {
				lb[r] = min(lb[r], lv);
				ub[r] = max(ub[r], lv);
			}
		}

		vector<pair<int, int>> rng;
		for(auto kv : lb)
			rng.emplace_back(kv.second, ub[kv.first]);
		int L = *max_element(level.begin(), level.end());
		return solve(rng, L);
	}

	// Number of ways to cover [0,L] by choosing subset of |rng|s.
	int solve(vector<pair<int, int>> rng, int L) {
		sort(rng.begin(), rng.end());

		function<int(int, int)> rec;
		vector<int> memo(rng.size() * (L+2), -1);
		rec = [&](int i, int coveredUpto) {
			if (i == rng.size())
				return coveredUpto == L ? 1 : 0;

			const int key = i*(L + 1) + (coveredUpto+1);
			if (memo[key] != -1)
				return memo[key];

			if (coveredUpto + 2 <= rng[i].first)
				return memo[key] = 0;
			return memo[key] = (rec(i + 1, coveredUpto) + rec(i + 1, max(coveredUpto, rng[i].second))) % MODVAL;
		};
		return rec(0, -1);
	}
};

// 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(_, DengklekGaneshAndTree().getCount(labels, parents));}
int main(){

CASE(0)
	string labels = "seems"; 
	int parents_[] = {0, 1, 0, 3};
	  vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); 
	int _ = 5; 
END
CASE(1)
	string labels = "like"; 
	int parents_[] = {0, 0, 0};
	  vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); 
	int _ = 7; 
END
CASE(2)
	string labels = "a"; 
	vector <int> parents; 
	int _ = 1; 
END
CASE(3)
	string labels = "coincidence"; 
	int parents_[] = {0, 1, 2, 0, 2, 1, 4, 7, 7, 6};
	  vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); 
	int _ = 218; 
END
CASE(4)
	string labels = "topcoderopenroundtwobgoodluckhavefun"; 
	int parents_[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
	  vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); 
	int _ = 147418098; 
END
/*
CASE(5)
	string labels = ; 
	int parents_[] = ;
	  vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); 
	int _ = ; 
END
CASE(6)
	string labels = ; 
	int parents_[] = ;
	  vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); 
	int _ = ; 
END
*/
}
// END CUT HERE