Artifact Content
Not logged in

Artifact 72fa1058f372c23c68984ae990295c6531e2687f


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

template<typename T>
struct DP2
{
	const int N1, N2;
	vector<T> data;
	DP2(int N1, int N2, const T& t = T())
		: N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<28)); }
	T& operator()(int i1, int i2)
		{ return data[ (i1*N2)+i2 ]; }
	void swap(DP2& rhs)
		{ data.swap(rhs.data); }
};

static const int INF = 0x3fffffff;
class FoxConnection { public:
	int minimalDistance(vector <int> A, vector <int> B, string haveFox)
	{
		int N = haveFox.size();
		vector<vector<int>> G(N);
		for(int i=0; i<A.size(); ++i) {
			int a = A[i] - 1;
			int b = B[i] - 1;
			G[a].push_back(b);
			G[b].push_back(a);
		}

		int best = INF;
		memo.resize(N+1, vector<vector<int>>(N));
		ts_memo.resize(N+1, vector<int>(N, INF));
		for(int root=0; root<N; ++root) {
			const vector<int>& L = rec(-1, root, G, haveFox);
			best = min(best, L[0]);
		}
		return best==INF ? 0 : best;
	}

	vector<vector<int>> ts_memo;
	int treeSize(int prev, int v, const vector<vector<int>>& G, const string& haveFox)
	{
		if(ts_memo[prev+1][v] != INF)
			return ts_memo[prev+1][v];

		int ts = (haveFox[v]=='Y');
		for(int u : G[v]) if(u != prev)
			ts += treeSize(v, u, G, haveFox);
		return ts_memo[prev+1][v] = ts;
	}

	vector<vector<vector<int>>> memo;
	const vector<int>& rec(int prev, int v, const vector<vector<int>>& G, const string& haveFox)
	{
		if(!memo[prev+1][v].empty())
			return memo[prev+1][v];
		int N = G.size();

		vector<const vector<int>*> children;
		for(int u : G[v]) if(u != prev)
			children.push_back(&rec(v,u,G,haveFox));
		int Z = children.size();

		DP2<int> dp(Z+1, N, INF);
		dp(0,0) = 0;
		for(int i=0; i<Z; ++i)
		{
			const vector<int>& ch = *children[i];
			for(int t=0; t<N; ++t)
			for(int w=0; w<N; ++w)
				if(t+w<N)
					dp(i+1, t+w) = min(dp(i+1, t+w), dp(i,t)+ch[w]);
		}

		const bool Y = (haveFox[v]=='Y');
		vector<int>& L = memo[prev+1][v];
		L.resize(N, INF);

		const int TS = treeSize(prev, v, G, haveFox);
		for(int i=0; i<N; ++i)
		{
			if(i == TS)
				L[i] = min(INF, TS + (Y ? dp(Z,TS-1) : dp(Z,TS)));
			else
				L[i] = min(INF, i + (Y ? dp(Z,i) : i+1<N ? dp(Z,i+1) : INF));
		}
		return 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(_, FoxConnection().minimalDistance(A, B, haveFox));}
int main(){

CASE(0)
	int A_[] = {1,2,3};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2,3,4};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = "YNNY"; 
	int _ = 2; 
END
CASE(1)
	int A_[] = {1,1,1,1};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2,3,4,5};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = "NYYYY"; 
	int _ = 1; 
END
CASE(2)
	int A_[] = {1,3,4,5,4};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2,2,2,4,6};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = "YNYNYY"; 
	int _ = 2; 
END
CASE(3)
	int A_[] = {1,2,3,4,5,6,7,8,9};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2,3,4,5,6,7,8,9,10};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = "YNNNYNYNNY"; 
	int _ = 7; 
END
CASE(4)
	int A_[] = {1,2,3,4,3,6,8,7};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2,3,4,5,6,8,9,6};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = "YNNYYNYYY"; 
	int _ = 3; 
END
CASE(5)
	int A_[] = {1};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = "NY"; 
	int _ = 0; 
END
CASE(6)
	int A_[] = {1};
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = {2};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = "NN"; 
	int _ = 0; 
END
/*
CASE(7)
	int A_[] = ;
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = ;
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = ; 
	int _ = ; 
END
CASE(8)
	int A_[] = ;
	  vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); 
	int B_[] = ;
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string haveFox = ; 
	int _ = ; 
END
*/
}
// END CUT HERE