Artifact Content
Not logged in

Artifact 3950e09c2280ab10cbba352867f1fe0934d60aa0


#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 TreeDistanceConstruction { public:
	vector <int> construct(vector <int> d)
	{
		map<int, vector<int>> d2i;
		for (int i = 0; i < d.size(); ++i)
			d2i[d[i]].push_back(i);

		const int m = d2i.begin()->first;
		const int M = d2i.rbegin()->first;
		vector<int> ans;

		if (d2i[m].size() >= 3)
			return vector<int>();

		int L = d2i[m].front(), R = d2i[m].back();
		if (d2i[m].size() == 1) {
			if (2 * m != M)
				return vector<int>();
		}
		else { // d2i[m].size() == 2
			if (2*m-1 != M)
				return vector<int>();
			ans.push_back(L);
			ans.push_back(R);
		}

		for (int k = m + 1; k <= M; ++k) {
			if (d2i[k].size() < 2)
				return vector<int>();
			for (int i = 0; i < d2i[k].size(); ++i) {
				ans.push_back(i == 0 ? L : R);
				ans.push_back(d2i[k][i]);
			}
			L = d2i[k].front();
			R = d2i[k].back();
		}
		return ans;
	}
};

// 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 vector <int>& Expected, const vector <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(_, TreeDistanceConstruction().construct(d));}
int main(){

CASE(0)
	int d_[] = {3,2,2,3};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int __[] = {1, 2, 1, 0, 2, 3 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	int d_[] = {1,2,2,2};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int __[] = {0, 1, 0, 2, 0, 3 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	int d_[] = {1,1,1,1};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	vector <int> _; 
END
CASE(3)
	int d_[] = {1,1,1};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	vector <int> _; 
END
CASE(4)
	int d_[] = {1,1};
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	int __[] = {0, 1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(5)
int d_[] = { 2,2,3,3,3 };
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	  int __[] = { -1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(6)
	int d_[] = { 3,4,4,5,6,6 };
	  vector <int> d(d_, d_+sizeof(d_)/sizeof(*d_)); 
	  int __[] = { -1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
}
// END CUT HERE