Artifact Content
Not logged in

Artifact 464024997191202d06a3994da0c512567c486ad9


#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 Ethernet { public:
	int connect(vector <int> parent, vector <int> dist, int maxDist)
	{
		int N = parent.size() + 1;
		vector<vector<pair<int,int>>> child(N);
		for(int i=0; i<parent.size(); ++i)
			child[parent[i]].emplace_back(i+1, dist[i]);
		auto cc = rec(0, child, maxDist);
		return cc.first + 1;
	}

	// (colors / critical path)
	pair<int, int> rec(int v, const vector<vector<pair<int,int>>>& child, int maxDist)
	{
		if(child[v].empty())
			return make_pair(0, 0);

		int colors = 0;
		vector<int> cps;
		for(auto& ue: child[v]) {
			int u = ue.first;
			int e = ue.second;
			auto cd = rec(u, child, maxDist);
			int c = cd.first;
			int d = cd.second;
			colors += c;
			if(e+d > maxDist)
				colors ++;
			else
				cps.push_back(e + d);
		}

		if(cps.empty())
			return make_pair(colors, 0);

		sort(cps.rbegin(), cps.rend());
		if(cps.size() > 1 && cps[0]+cps[1] > maxDist)
			for(int i=0; i<cps.size(); ++i)
				if(i+1==cps.size() || cps[i]+cps[i+1] <= maxDist)
					return make_pair(colors+i, cps[i]);

		// no new color needed.
		return make_pair(colors, cps[0]);
	}
};

// 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(_, Ethernet().connect(parent, dist, maxDist));}
int main(){
	CASE(2)
	int parent_[] = {0,1,2,3,4,5};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int dist_[] = {1,2,3,4,5,6};
	  vector <int> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int maxDist = 6; 
	int _ = 3; 
END
CASE(0)
	int parent_[] = {0,0,0};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int dist_[] = {1,1,1};
	  vector <int> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int maxDist = 2; 
	int _ = 1; 
END
CASE(1)
	int parent_[] = {0,0,0,0,0,0,0};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int dist_[] = {1,2,3,4,5,6,7};
	  vector <int> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int maxDist = 8; 
	int _ = 4; 
END
CASE(3)
	int parent_[] = {0,0,0,1,1};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int dist_[] = {1,1,1,1,1};
	  vector <int> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int maxDist = 2; 
	int _ = 2; 
END
CASE(4)
	int parent_[] = {0,1,0,3,0,5,0,6,0,6,0,6,4,6,9,4,5,5,2,5,2};
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int dist_[] = {93,42,104,105,59,73,161,130,30,81,62,93,131,133,139,5,13,34,25,111,4};
	  vector <int> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int maxDist = 162; 
	int _ = 11; 
END
/*
CASE(5)
	int parent_[] = ;
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int dist_[] = ;
	  vector <int> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int maxDist = ; 
	int _ = ; 
END
CASE(6)
	int parent_[] = ;
	  vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); 
	int dist_[] = ;
	  vector <int> dist(dist_, dist_+sizeof(dist_)/sizeof(*dist_)); 
	int maxDist = ; 
	int _ = ; 
END
*/
}
// END CUT HERE