Artifact Content
Not logged in

Artifact cfb3baca4827166b1b78fe1b28932addafbb5c97


#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 DoubleTree { public:
	int maximalScore(vector <int> a, vector <int> b, vector <int> c, vector <int> d, vector <int> score)
	{
		const int N = a.size() + 1;

		vector<vector<LL>> dep(N, vector<LL>(N));
		add_dep(a, b, dep);
		add_dep(c, d, dep);
		closure(N, dep);

		int best = 0;
		for(int root=0; root<N; ++root)
		{
			int base_score = score[root];

			set<LL> cs;
			for(int v=0; v<N; ++v) if(v!=root)
				cs.insert(dep[min(root,v)][max(root,v)] &~ (1LL<<root));

			LL theSet = 0;
			for(LL c: cs) {
				int ss = 0;
				for(int i=0; (1LL<<i)<=c; ++i)
					if((1LL<<i)&c)
						ss += score[i];
				if(ss>0)
					theSet |= c;
			}

			int ss = 0;
			for(int i=0; (1LL<<i)<=theSet; ++i)
				if((1LL<<i)&theSet)
					ss += score[i];
			best = max(best, base_score+ss);
		}
		return best;
	}

	void closure(int N, vector<vector<LL>>& dep)
	{
		for(bool upd=true; upd; ) {
			upd = false;
			for(int a=0; a<N; ++a)
			for(int b=a; b<N; ++b)
			for(int c=0; c<N; ++c) if((1LL<<c)&dep[a][b]) {
				LL neo = dep[a][b] | dep[min(a,c)][max(a,c)] | dep[min(b,c)][max(b,c)];
				if(neo != dep[a][b]) {
					upd = true;
					dep[a][b] = neo;
				}
			}
		}
	}

	void add_dep(const vector<int>& a, const vector<int>& b, vector<vector<LL>>& dep)
	{
		int N = a.size() + 1;
		vector<vector<int>> G(N);
		for(int i=0; i<N-1; ++i) {
			G[a[i]].push_back(b[i]);
			G[b[i]].push_back(a[i]);
		}

		for(int root=0; root<N; ++root) {
			vector<int> stk;
			function<void(int,int)> rec;
			rec = [&](int pre, int v) {
				stk.push_back(v);
				for(int mid: stk)
					dep[min(root,v)][max(root,v)] |= 1LL<<mid;
				for(int u: G[v]) if(u!=pre)
					rec(v, u);
				stk.pop_back();
			};
			rec(-1, root);
		}
	}
};

// 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(_, DoubleTree().maximalScore(a, b, c, d, score));}
int main(){

CASE(0)
	int a_[] = {0,0,1};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1,3,2};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int c_[] = {0,0,3};
	  vector <int> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	int d_[] = {1,3,2};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int score_[] = {1000,24,100,-200};
	  vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int _ = 1024; 
END
CASE(1)
	int a_[] = {0,0,1};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1,3,2};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int c_[] = {0,0,3};
	  vector <int> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	int d_[] = {1,3,2};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int score_[] = {1000,24,100,200};
	  vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int _ = 1324; 
END
CASE(2)
	int a_[] = {0,0,1};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1,3,2};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int c_[] = {0,0,3};
	  vector <int> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	int d_[] = {1,3,2};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int score_[] = {-1000,-24,-100,-200};
	  vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int _ = 0; 
END
CASE(3)
	int a_[] = {0,0,1};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1,3,2};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int c_[] = {0,0,3};
	  vector <int> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	int d_[] = {1,3,2};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int score_[] = {-1000,24,100,200};
	  vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int _ = 200; 
END
CASE(4)
	int a_[] = {0,0,1,1,2,2};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1,2,3,4,5,6};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int c_[] = {0,0,1,1,2,2};
	  vector <int> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	int d_[] = {1,2,3,4,5,6};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int score_[] = {-3,2,2,-1,2,2,-1};
	  vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int _ = 5; 
END
CASE(5)
	int a_[] = {0,0,1,1,2,2};
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = {1,2,3,4,5,6};
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int c_[] = {0,0,0,0,0,0};
	  vector <int> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	int d_[] = {1,2,3,4,5,6};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int score_[] = {-3,2,2,-1,2,2,-1};
	  vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int _ = 5; 
END
/*
CASE(6)
	int a_[] = ;
	  vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int b_[] = ;
	  vector <int> b(b_, b_+sizeof(b_)/sizeof(*b_)); 
	int c_[] = ;
	  vector <int> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	int d_[] = ;
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int score_[] = ;
	  vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int _ = ; 
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_)); 
	int c_[] = ;
	  vector <int> c(c_, c_+sizeof(c_)/sizeof(*c_)); 
	int d_[] = ;
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int score_[] = ;
	  vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int _ = ; 
END
*/
}
// END CUT HERE