d335067c64 2017-07-08 kinaba: #include <iostream> d335067c64 2017-07-08 kinaba: #include <sstream> d335067c64 2017-07-08 kinaba: #include <iomanip> d335067c64 2017-07-08 kinaba: #include <vector> d335067c64 2017-07-08 kinaba: #include <string> d335067c64 2017-07-08 kinaba: #include <map> d335067c64 2017-07-08 kinaba: #include <set> d335067c64 2017-07-08 kinaba: #include <algorithm> d335067c64 2017-07-08 kinaba: #include <numeric> d335067c64 2017-07-08 kinaba: #include <iterator> d335067c64 2017-07-08 kinaba: #include <functional> d335067c64 2017-07-08 kinaba: #include <complex> d335067c64 2017-07-08 kinaba: #include <queue> d335067c64 2017-07-08 kinaba: #include <stack> d335067c64 2017-07-08 kinaba: #include <cmath> d335067c64 2017-07-08 kinaba: #include <cassert> d335067c64 2017-07-08 kinaba: #include <tuple> d335067c64 2017-07-08 kinaba: using namespace std; d335067c64 2017-07-08 kinaba: typedef long long LL; d335067c64 2017-07-08 kinaba: typedef complex<double> CMP; d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: struct UnionFind d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: vector<int> uf, sz; d335067c64 2017-07-08 kinaba: int nc; d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: UnionFind(int N) : uf(N), sz(N, 1), nc(N) d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: for (int i = 0; i<N; ++i) uf[i] = i; d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: int size() d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: return nc; d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: int size(int a) d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: return sz[Find(a)]; d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: int Find(int a) d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: return uf[a] == a ? a : uf[a] = Find(uf[a]); d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: // Semi-compression w.o. recursion. d335067c64 2017-07-08 kinaba: //{ while(uf[a] != a) a=uf[a]=uf[uf[a]]; return a; } d335067c64 2017-07-08 kinaba: bool Union(int a, int b) d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: a = Find(a); d335067c64 2017-07-08 kinaba: b = Find(b); d335067c64 2017-07-08 kinaba: if (a != b) d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: if (sz[a] >= sz[b]) swap(a, b); d335067c64 2017-07-08 kinaba: uf[a] = b; d335067c64 2017-07-08 kinaba: sz[b] += sz[a]; d335067c64 2017-07-08 kinaba: --nc; d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: return (a != b); d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: }; d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: static const unsigned MODVAL = 1000000007; d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: class DengklekGaneshAndTree { public: d335067c64 2017-07-08 kinaba: int getCount(string labels, vector <int> parents) d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: const int N = labels.size(); d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: vector<vector<int>> children(N); d335067c64 2017-07-08 kinaba: UnionFind uf(N); d335067c64 2017-07-08 kinaba: for (int i = 1; i < N; ++i) { d335067c64 2017-07-08 kinaba: int u = parents[i-1], v = i; d335067c64 2017-07-08 kinaba: children[u].push_back(v); d335067c64 2017-07-08 kinaba: if (labels[u] == labels[v]) d335067c64 2017-07-08 kinaba: uf.Union(u, v); d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: vector<int> level(N); d335067c64 2017-07-08 kinaba: { d335067c64 2017-07-08 kinaba: function<void(int, int)> rec; rec = [&](int v, int lv) { d335067c64 2017-07-08 kinaba: level[v] = lv; d335067c64 2017-07-08 kinaba: for (int u : children[v]) d335067c64 2017-07-08 kinaba: rec(u, lv + 1); d335067c64 2017-07-08 kinaba: }; d335067c64 2017-07-08 kinaba: rec(0, 0); d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: map<int, int> lb, ub; d335067c64 2017-07-08 kinaba: for (int v = 0; v < N; ++v) { d335067c64 2017-07-08 kinaba: int r = uf.Find(v), lv = level[v]; d335067c64 2017-07-08 kinaba: if (lb.count(r) == 0) { d335067c64 2017-07-08 kinaba: lb[r] = lv; d335067c64 2017-07-08 kinaba: ub[r] = lv; d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: else { d335067c64 2017-07-08 kinaba: lb[r] = min(lb[r], lv); d335067c64 2017-07-08 kinaba: ub[r] = max(ub[r], lv); d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: vector<pair<int, int>> rng; d335067c64 2017-07-08 kinaba: for(auto kv : lb) d335067c64 2017-07-08 kinaba: rng.emplace_back(kv.second, ub[kv.first]); d335067c64 2017-07-08 kinaba: int L = *max_element(level.begin(), level.end()); d335067c64 2017-07-08 kinaba: return solve(rng, L); d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: // Number of ways to cover [0,L] by choosing subset of |rng|s. d335067c64 2017-07-08 kinaba: int solve(vector<pair<int, int>> rng, int L) { d335067c64 2017-07-08 kinaba: sort(rng.begin(), rng.end()); d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: function<int(int, int)> rec; d335067c64 2017-07-08 kinaba: vector<int> memo(rng.size() * (L+2), -1); d335067c64 2017-07-08 kinaba: rec = [&](int i, int coveredUpto) { d335067c64 2017-07-08 kinaba: if (i == rng.size()) d335067c64 2017-07-08 kinaba: return coveredUpto == L ? 1 : 0; d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: const int key = i*(L + 1) + (coveredUpto+1); d335067c64 2017-07-08 kinaba: if (memo[key] != -1) d335067c64 2017-07-08 kinaba: return memo[key]; d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: if (coveredUpto + 2 <= rng[i].first) d335067c64 2017-07-08 kinaba: return memo[key] = 0; d335067c64 2017-07-08 kinaba: return memo[key] = (rec(i + 1, coveredUpto) + rec(i + 1, max(coveredUpto, rng[i].second))) % MODVAL; d335067c64 2017-07-08 kinaba: }; d335067c64 2017-07-08 kinaba: return rec(0, -1); d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: }; d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: // BEGIN CUT HERE d335067c64 2017-07-08 kinaba: #include <ctime> d335067c64 2017-07-08 kinaba: double start_time; string timer() d335067c64 2017-07-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d335067c64 2017-07-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d335067c64 2017-07-08 kinaba: { os << "{ "; d335067c64 2017-07-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d335067c64 2017-07-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d335067c64 2017-07-08 kinaba: void verify_case(const int& Expected, const int& Received) { d335067c64 2017-07-08 kinaba: bool ok = (Expected == Received); d335067c64 2017-07-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d335067c64 2017-07-08 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d335067c64 2017-07-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d335067c64 2017-07-08 kinaba: #define END verify_case(_, DengklekGaneshAndTree().getCount(labels, parents));} d335067c64 2017-07-08 kinaba: int main(){ d335067c64 2017-07-08 kinaba: d335067c64 2017-07-08 kinaba: CASE(0) d335067c64 2017-07-08 kinaba: string labels = "seems"; d335067c64 2017-07-08 kinaba: int parents_[] = {0, 1, 0, 3}; d335067c64 2017-07-08 kinaba: vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); d335067c64 2017-07-08 kinaba: int _ = 5; d335067c64 2017-07-08 kinaba: END d335067c64 2017-07-08 kinaba: CASE(1) d335067c64 2017-07-08 kinaba: string labels = "like"; d335067c64 2017-07-08 kinaba: int parents_[] = {0, 0, 0}; d335067c64 2017-07-08 kinaba: vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); d335067c64 2017-07-08 kinaba: int _ = 7; d335067c64 2017-07-08 kinaba: END d335067c64 2017-07-08 kinaba: CASE(2) d335067c64 2017-07-08 kinaba: string labels = "a"; d335067c64 2017-07-08 kinaba: vector <int> parents; d335067c64 2017-07-08 kinaba: int _ = 1; d335067c64 2017-07-08 kinaba: END d335067c64 2017-07-08 kinaba: CASE(3) d335067c64 2017-07-08 kinaba: string labels = "coincidence"; d335067c64 2017-07-08 kinaba: int parents_[] = {0, 1, 2, 0, 2, 1, 4, 7, 7, 6}; d335067c64 2017-07-08 kinaba: vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); d335067c64 2017-07-08 kinaba: int _ = 218; d335067c64 2017-07-08 kinaba: END d335067c64 2017-07-08 kinaba: CASE(4) d335067c64 2017-07-08 kinaba: string labels = "topcoderopenroundtwobgoodluckhavefun"; d335067c64 2017-07-08 kinaba: 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}; d335067c64 2017-07-08 kinaba: vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); d335067c64 2017-07-08 kinaba: int _ = 147418098; d335067c64 2017-07-08 kinaba: END d335067c64 2017-07-08 kinaba: /* d335067c64 2017-07-08 kinaba: CASE(5) d335067c64 2017-07-08 kinaba: string labels = ; d335067c64 2017-07-08 kinaba: int parents_[] = ; d335067c64 2017-07-08 kinaba: vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); d335067c64 2017-07-08 kinaba: int _ = ; d335067c64 2017-07-08 kinaba: END d335067c64 2017-07-08 kinaba: CASE(6) d335067c64 2017-07-08 kinaba: string labels = ; d335067c64 2017-07-08 kinaba: int parents_[] = ; d335067c64 2017-07-08 kinaba: vector <int> parents(parents_, parents_+sizeof(parents_)/sizeof(*parents_)); d335067c64 2017-07-08 kinaba: int _ = ; d335067c64 2017-07-08 kinaba: END d335067c64 2017-07-08 kinaba: */ d335067c64 2017-07-08 kinaba: } d335067c64 2017-07-08 kinaba: // END CUT HERE