/** Fetch an element from the cache. * If the key does not exist yet, a new empty element will be * created. */ mapped_type& operator[] (const key_type& k) { auto found (map_.find(k)); if (found == map_.end()) { list_.push_front(pair_t(k, mapped_type())); map_[k] = list_.begin(); ++size_; return list_.begin()->second; } list_.splice(list_.begin(), list_, found->second); return found->second->second; }
/** Mark an element as recently used. */ void touch (const key_type& k) { auto found (map_.find(k)); assert(found != map_.end()); if (found != map_.end()) list_.splice(list_.begin(), list_, found->second); }
boost::optional<mapped_type&> try_get (const key_type& k) { auto found (map_.find(k)); if (found == map_.end()) return boost::optional<mapped_type&>(); list_.splice(list_.begin(), list_, found->second); return found->second->second; }
static void remove_even(list_t &l) { bool even = false; for (auto it = l.begin(); it != l.end();) { if (even) it = l.erase(it); else ++it; even = !even; } }
void prune_if (size_t max_size, pred op) { if (list_.empty()) return; auto i (std::prev(list_.end())); while (size_ > max_size) { if (op(*i)) { map_.erase(i->first); i = list_.erase(i); --size_; } if (i == list_.begin()) return; --i; } }
void comb(list_t &L,traverse_tree_fun f,void *data=NULL) { if (L.size()==1) { f(*L.begin(),data); return; } int n=L.size(); for (int j=0; j<n-1; j++) { for (int k=j+1; k<n; k++) { btree_t T; T.insert(T.begin(),-1); node_t m = T.begin(); pos_t pk=L.begin(); for (int kk=0; kk<k; kk++) pk++; T.splice(m.left(),pk->begin()); L.erase(pk); pos_t pj=L.begin(); for (int jj=0; jj<j; jj++) pj++; T.splice(m.right(),pj->begin()); L.erase(pj); pos_t p = L.insert(L.begin(),btree_t()); p->splice(p->begin(),T.begin()); comb(L,f,data); p = L.begin(); m = T.splice(T.begin(),p->begin()); L.erase(p); pj=L.begin(); for (int jj=0; jj<j; jj++) pj++; pj = L.insert(pj,btree_t()); pj->splice(pj->begin(),m.right()); pk=L.begin(); for (int kk=0; kk<k; kk++) pk++; pk = L.insert(pk,btree_t()); pk->splice(pk->begin(),m.left()); } } }
void solve(int dataId) { int s = -1, t= -1, use = -1; for(int i=0;i<26;i++) { int outd = 0, ind=0; for(int j=0;j<26;j++) { outd += links[i][j].size(); ind += links[j][i].size(); } if(outd || ind) use = i; if(outd > ind+1 || ind > outd+1) { cout << "IMPOSSIBLE" << endl; return; } if(outd > ind) { if(s != -1) { cout << "IMPOSSIBLE" << endl; return; } s = i; } if(ind > outd) { if(t != -1) { cout << "IMPOSSIBLE" << endl; return; } t = i; } } if(s == -1) s = t = use; if(walk2(output.end(), s, t) == -1) { cout << "IMPOSSIBLE" << endl; return; } output.pb(mp("", t)); foreach(it, output) { if(pump(it) == -1) { cout << "IMPOSSIBLE" << endl; return; } } for(int i=0;i<30;i++) for(int j=0;j<30;j++) if(links[i][j].size()) { cout << "IMPOSSIBLE" << endl; return; } output.pop_back(); list_t::iterator it = output.begin(); do { cout << it->first; ++it; if(it == output.end()) break; cout << " "; } while(true); cout << endl; }