Artifact Content
Not logged in

Artifact 5237c6bbcc22042e8c42ba75f21a92e76475a922


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

class WalkOverATree { public:
	int maxNodesVisited(vector <int> parent, int L)
	{
		const int N = parent.size() + 1;
		vector<vector<int>> G(N);
		for(int i=0; i<parent.size(); ++i)
			G[parent[i]].push_back(i+1);

		vector<int> numBelow(N); {
			function<int(int)> rec;
			rec = [&](int v) {
				int cnt = 1;
				for(auto u: G[v])
					cnt += rec(u);
				return numBelow[v] = cnt;
			};
			rec(0);
		}

		function<int(int, int)> rec;
		map<pair<int,int>, int> memo;
		rec = [&](int v, int L) {
			const pair<int,int> key(v, L);
			if(memo.count(key)) return memo[key];

			const vector<int>& child = G[v];
			if(L<=0 || child.empty())
				return 1;

			int visMax = 0;
			for(int i=0; i<child.size(); ++i) {
				set<int> previsit;
				previsit.insert(0);
				for(int k=0; k<child.size(); ++k) if(i!=k) {
					set<int> pv2 = previsit;
					for(int z: previsit)
						pv2.insert(z + numBelow[child[k]]);
					previsit = pv2;
				}

				for(int p: previsit) if(p*2 <= L)
					visMax = max(visMax, 1 + p + rec(child[i], L-p*2-1));
			}
			return memo[key] = visMax;
		};
		return rec(0, L);
	}
};

// 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(_, WalkOverATree().maxNodesVisited(parent, L));}
int main(){
/*
CASE(0)
	int parent_[] = {0, 0};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 2; 
	int _ = 2; 
END
*/
CASE(1)
	int parent_[] = {0, 0};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 3; 
	int _ = 3; 
END
CASE(2)
	int parent_[] = {0, 1, 2, 3};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 2; 
	int _ = 3; 
END
CASE(3)
	int parent_[] = {0,0,0,0,2,4,2,3,1};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 1; 
	int _ = 2; 
END
CASE(4)
	int parent_[] = {0,0,1,2,3,2,3,1,3,0,1,8,6,8,0,5,15,0,9};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 4; 
	int _ = 5; 
END
CASE(5)
	int parent_[] = {0,0,0,1,1,3,5,1,4,5,2,2,10,5,10,10,11,13,8,3,18,15,20,20,23,8,11,26,4};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 26; 
	int _ = 17; 
END
CASE(6)
	int parent_[] = {0, 0, 2, 0}
;
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 100; 
	int _ = 5; 
END
CASE(7)
	int parent_[] = {0, 0, 2};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 4; 
	int _ = 4; 
END
CASE(8)
	int parent_[] = {0,1,2,0,4,4,4,4};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 3; 
	int _ = 4; 
END
CASE(9)
	int parent_[] = {0,0,2,0,4,5};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int L = 7; 
	int _ = 6; 
END
}
// END CUT HERE