5cbddaf9a4 2017-05-20 kinaba: #include <iostream> 5cbddaf9a4 2017-05-20 kinaba: #include <sstream> 5cbddaf9a4 2017-05-20 kinaba: #include <iomanip> 5cbddaf9a4 2017-05-20 kinaba: #include <vector> 5cbddaf9a4 2017-05-20 kinaba: #include <string> 5cbddaf9a4 2017-05-20 kinaba: #include <map> 5cbddaf9a4 2017-05-20 kinaba: #include <set> 5cbddaf9a4 2017-05-20 kinaba: #include <algorithm> 5cbddaf9a4 2017-05-20 kinaba: #include <numeric> 5cbddaf9a4 2017-05-20 kinaba: #include <iterator> 5cbddaf9a4 2017-05-20 kinaba: #include <functional> 5cbddaf9a4 2017-05-20 kinaba: #include <complex> 5cbddaf9a4 2017-05-20 kinaba: #include <queue> 5cbddaf9a4 2017-05-20 kinaba: #include <stack> 5cbddaf9a4 2017-05-20 kinaba: #include <cmath> 5cbddaf9a4 2017-05-20 kinaba: #include <cassert> 5cbddaf9a4 2017-05-20 kinaba: #include <tuple> 5cbddaf9a4 2017-05-20 kinaba: using namespace std; 5cbddaf9a4 2017-05-20 kinaba: typedef long long LL; 5cbddaf9a4 2017-05-20 kinaba: typedef complex<double> CMP; 5cbddaf9a4 2017-05-20 kinaba: 5cbddaf9a4 2017-05-20 kinaba: class DistanceZeroAndOne { public: 5cbddaf9a4 2017-05-20 kinaba: vector <string> construct(vector <int> dist0, vector <int> dist1) 5cbddaf9a4 2017-05-20 kinaba: { 5cbddaf9a4 2017-05-20 kinaba: const vector<string> IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: const int N = dist0.size(); 5cbddaf9a4 2017-05-20 kinaba: const int d01 = dist0[1]; 5cbddaf9a4 2017-05-20 kinaba: if (dist0[0] != 0) return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: if (dist1[1] != 0) return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: if (dist0[1] != dist1[0]) return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: 5cbddaf9a4 2017-05-20 kinaba: map<pair<int, int>, int> repr; 5cbddaf9a4 2017-05-20 kinaba: for (int x = 0; x < N; ++x) 5cbddaf9a4 2017-05-20 kinaba: repr[make_pair(dist0[x], dist1[x])] = x; 5cbddaf9a4 2017-05-20 kinaba: 5cbddaf9a4 2017-05-20 kinaba: vector<string> ans(N, string(N, 'N')); 5cbddaf9a4 2017-05-20 kinaba: ans[0][1] = ans[1][0] = d01 == 1 ? 'Y' : 'N'; 5cbddaf9a4 2017-05-20 kinaba: for (int x = 2; x < N; ++x) { 5cbddaf9a4 2017-05-20 kinaba: const int d0 = dist0[x]; 5cbddaf9a4 2017-05-20 kinaba: const int d1 = dist1[x]; 5cbddaf9a4 2017-05-20 kinaba: if (d0==0 || d1==0 || d0+d1<d01) 5cbddaf9a4 2017-05-20 kinaba: return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: if (d0 == 1) ans[0][x] = ans[x][0] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: if (d1 == 1) ans[1][x] = ans[x][1] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: 5cbddaf9a4 2017-05-20 kinaba: if (d0 + d1 == d01) { // bone 5cbddaf9a4 2017-05-20 kinaba: if (repr.count(make_pair(d0 - 1, d1 + 1))) { 5cbddaf9a4 2017-05-20 kinaba: int p = repr[make_pair(d0 - 1, d1 + 1)]; 5cbddaf9a4 2017-05-20 kinaba: ans[p][x] = ans[x][p] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: else { 5cbddaf9a4 2017-05-20 kinaba: return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: 5cbddaf9a4 2017-05-20 kinaba: if (repr.count(make_pair(d0 + 1, d1 - 1))) { 5cbddaf9a4 2017-05-20 kinaba: int p = repr[make_pair(d0 + 1, d1 - 1)]; 5cbddaf9a4 2017-05-20 kinaba: ans[p][x] = ans[x][p] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: else { 5cbddaf9a4 2017-05-20 kinaba: return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: else if(abs(d0 - d1) == d01) { // d0/d1 children 5cbddaf9a4 2017-05-20 kinaba: if (repr.count(make_pair(d0 - 1, d1 - 1))) { 5cbddaf9a4 2017-05-20 kinaba: int p = repr[make_pair(d0 - 1, d1 - 1)]; 5cbddaf9a4 2017-05-20 kinaba: ans[p][x] = ans[x][p] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: else { 5cbddaf9a4 2017-05-20 kinaba: return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: else { 5cbddaf9a4 2017-05-20 kinaba: // mid 5cbddaf9a4 2017-05-20 kinaba: if (repr.count(make_pair(d0 - 1, d1 - 1))) { 5cbddaf9a4 2017-05-20 kinaba: int p = repr[make_pair(d0 - 1, d1 - 1)]; 5cbddaf9a4 2017-05-20 kinaba: ans[p][x] = ans[x][p] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: else { 5cbddaf9a4 2017-05-20 kinaba: if (repr.count(make_pair(d0 - 1, d1 + 1))) { 5cbddaf9a4 2017-05-20 kinaba: int p = repr[make_pair(d0 - 1, d1 + 1)]; 5cbddaf9a4 2017-05-20 kinaba: ans[p][x] = ans[x][p] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: else if (repr.count(make_pair(d0 - 1, d1))) { 5cbddaf9a4 2017-05-20 kinaba: int p = repr[make_pair(d0 - 1, d1)]; 5cbddaf9a4 2017-05-20 kinaba: ans[p][x] = ans[x][p] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: } else { 5cbddaf9a4 2017-05-20 kinaba: return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: 5cbddaf9a4 2017-05-20 kinaba: if (repr.count(make_pair(d0 + 1, d1 - 1))) { 5cbddaf9a4 2017-05-20 kinaba: int p = repr[make_pair(d0 + 1, d1 - 1)]; 5cbddaf9a4 2017-05-20 kinaba: ans[p][x] = ans[x][p] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: else if (repr.count(make_pair(d0, d1 - 1))) { 5cbddaf9a4 2017-05-20 kinaba: int p = repr[make_pair(d0, d1 - 1)]; 5cbddaf9a4 2017-05-20 kinaba: ans[p][x] = ans[x][p] = 'Y'; 5cbddaf9a4 2017-05-20 kinaba: } else { 5cbddaf9a4 2017-05-20 kinaba: return IMPOSSIBLE; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: return ans; 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: }; 5cbddaf9a4 2017-05-20 kinaba: 5cbddaf9a4 2017-05-20 kinaba: // BEGIN CUT HERE 5cbddaf9a4 2017-05-20 kinaba: #include <ctime> 5cbddaf9a4 2017-05-20 kinaba: double start_time; string timer() 5cbddaf9a4 2017-05-20 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 5cbddaf9a4 2017-05-20 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 5cbddaf9a4 2017-05-20 kinaba: { os << "{ "; 5cbddaf9a4 2017-05-20 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 5cbddaf9a4 2017-05-20 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 5cbddaf9a4 2017-05-20 kinaba: void verify_case(const vector <string>& Expected, const vector <string>& Received) { 5cbddaf9a4 2017-05-20 kinaba: bool ok = (Expected == Received); 5cbddaf9a4 2017-05-20 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 5cbddaf9a4 2017-05-20 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } 5cbddaf9a4 2017-05-20 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 5cbddaf9a4 2017-05-20 kinaba: #define END verify_case(_, DistanceZeroAndOne().construct(dist0, dist1));} 5cbddaf9a4 2017-05-20 kinaba: int main(){ 5cbddaf9a4 2017-05-20 kinaba: 5cbddaf9a4 2017-05-20 kinaba: CASE(0) 5cbddaf9a4 2017-05-20 kinaba: int dist0_[] = {0,2,1}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist0(dist0_, dist0_+sizeof(dist0_)/sizeof(*dist0_)); 5cbddaf9a4 2017-05-20 kinaba: int dist1_[] = {2,0,1}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist1(dist1_, dist1_+sizeof(dist1_)/sizeof(*dist1_)); 5cbddaf9a4 2017-05-20 kinaba: string __[] = {"NNY", "NNY", "YYN" }; 5cbddaf9a4 2017-05-20 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); 5cbddaf9a4 2017-05-20 kinaba: END 5cbddaf9a4 2017-05-20 kinaba: CASE(1) 5cbddaf9a4 2017-05-20 kinaba: int dist0_[] = {0,2,1}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist0(dist0_, dist0_+sizeof(dist0_)/sizeof(*dist0_)); 5cbddaf9a4 2017-05-20 kinaba: int dist1_[] = {1,0,2}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist1(dist1_, dist1_+sizeof(dist1_)/sizeof(*dist1_)); 5cbddaf9a4 2017-05-20 kinaba: vector <string> _; 5cbddaf9a4 2017-05-20 kinaba: END 5cbddaf9a4 2017-05-20 kinaba: CASE(2) 5cbddaf9a4 2017-05-20 kinaba: int dist0_[] = {3,1,1,1}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist0(dist0_, dist0_+sizeof(dist0_)/sizeof(*dist0_)); 5cbddaf9a4 2017-05-20 kinaba: int dist1_[] = {1,0,1,1}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist1(dist1_, dist1_+sizeof(dist1_)/sizeof(*dist1_)); 5cbddaf9a4 2017-05-20 kinaba: vector <string> _; 5cbddaf9a4 2017-05-20 kinaba: END 5cbddaf9a4 2017-05-20 kinaba: CASE(3) 5cbddaf9a4 2017-05-20 kinaba: int dist0_[] = {0,1,1,1}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist0(dist0_, dist0_+sizeof(dist0_)/sizeof(*dist0_)); 5cbddaf9a4 2017-05-20 kinaba: int dist1_[] = {1,0,1,1}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist1(dist1_, dist1_+sizeof(dist1_)/sizeof(*dist1_)); 5cbddaf9a4 2017-05-20 kinaba: string __[] = {"NYYY", "YNYY", "YYNN", "YYNN" }; 5cbddaf9a4 2017-05-20 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); 5cbddaf9a4 2017-05-20 kinaba: END 5cbddaf9a4 2017-05-20 kinaba: CASE(4) 5cbddaf9a4 2017-05-20 kinaba: int dist0_[] = {0,3,1,2,2,3,4,4}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist0(dist0_, dist0_+sizeof(dist0_)/sizeof(*dist0_)); 5cbddaf9a4 2017-05-20 kinaba: int dist1_[] = {3,0,2,1,2,3,4,4}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist1(dist1_, dist1_+sizeof(dist1_)/sizeof(*dist1_)); 5cbddaf9a4 2017-05-20 kinaba: string __[] = {"NNYNNNNN", "NNNYNNNN", "YNNYYNNN", "NYYNYNNN", "NNYYNYNN", "NNNNYNYY", "NNNNNYNN", "NNNNNYNN" }; 5cbddaf9a4 2017-05-20 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); 5cbddaf9a4 2017-05-20 kinaba: END 5cbddaf9a4 2017-05-20 kinaba: CASE(5) 5cbddaf9a4 2017-05-20 kinaba: int dist0_[] = {0,1}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist0(dist0_, dist0_+sizeof(dist0_)/sizeof(*dist0_)); 5cbddaf9a4 2017-05-20 kinaba: int dist1_[] = {1,0}; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist1(dist1_, dist1_+sizeof(dist1_)/sizeof(*dist1_)); 5cbddaf9a4 2017-05-20 kinaba: string __[] = {"NY", "YN" }; 5cbddaf9a4 2017-05-20 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); 5cbddaf9a4 2017-05-20 kinaba: END 5cbddaf9a4 2017-05-20 kinaba: /* 5cbddaf9a4 2017-05-20 kinaba: CASE(6) 5cbddaf9a4 2017-05-20 kinaba: int dist0_[] = ; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist0(dist0_, dist0_+sizeof(dist0_)/sizeof(*dist0_)); 5cbddaf9a4 2017-05-20 kinaba: int dist1_[] = ; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist1(dist1_, dist1_+sizeof(dist1_)/sizeof(*dist1_)); 5cbddaf9a4 2017-05-20 kinaba: string __[] = ; 5cbddaf9a4 2017-05-20 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); 5cbddaf9a4 2017-05-20 kinaba: END 5cbddaf9a4 2017-05-20 kinaba: CASE(7) 5cbddaf9a4 2017-05-20 kinaba: int dist0_[] = ; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist0(dist0_, dist0_+sizeof(dist0_)/sizeof(*dist0_)); 5cbddaf9a4 2017-05-20 kinaba: int dist1_[] = ; 5cbddaf9a4 2017-05-20 kinaba: vector <int> dist1(dist1_, dist1_+sizeof(dist1_)/sizeof(*dist1_)); 5cbddaf9a4 2017-05-20 kinaba: string __[] = ; 5cbddaf9a4 2017-05-20 kinaba: vector <string> _(__, __+sizeof(__)/sizeof(*__)); 5cbddaf9a4 2017-05-20 kinaba: END 5cbddaf9a4 2017-05-20 kinaba: */ 5cbddaf9a4 2017-05-20 kinaba: } 5cbddaf9a4 2017-05-20 kinaba: // END CUT HERE