classPQTree{public:PQTree(){}voidInit(intn){n_=n,rt_=tot_=n+1;for(inti=1;i<=n;i++)g_[rt_].emplace_back(i);}voidInsert(conststd::string&s){s_=s;Dfs0(rt_);Work(rt_);while(g_[rt_].size()==1)rt_=g_[rt_][0];Remove(rt_);}std::vector<int>ans(){DfsAns(rt_);returnans_;}~PQTree(){}private:intn_,rt_,tot_,pool_[100001],top_,typ_[100001]/* 0-P 1-Q */,col_[100001]/* 0-black 1-white 2-grey */;std::vector<int>g_[100001],ans_;std::strings_;voidFail(){std::cout<<"NO\n";std::exit(0);}intNewNode(intty){intx=top_?pool_[top_--]:++tot_;typ_[x]=ty;returnx;}voidDelete(intu){g_[u].clear(),pool_[++top_]=u;}voidDfs0(intu){// get color of each nodeif(u>=1&&u<=n_){col_[u]=s_[u]=='1';return;}boolc0=false,c1=false;for(auto&&v:g_[u]){Dfs0(v);if(col_[v])c1=true;if(col_[v]!=1)c0=true;}if(c0&&!c1)col_[u]=0;elseif(!c0&&c1)col_[u]=1;elsecol_[u]=2;}boolCheck(conststd::vector<int>&v){intp2=-1;for(inti=0;i<static_cast<int>(v.size());i++)if(col_[v[i]]==2){if(p2!=-1)returnfalse;p2=i;}if(p2==-1)for(inti=0;i<static_cast<int>(v.size());i++)if(col_[v[i]]){p2=i;break;}for(inti=0;i<p2;i++)if(col_[v[i]])returnfalse;for(inti=p2+1;i<static_cast<int>(v.size());i++)if(col_[v[i]]!=1)returnfalse;returntrue;}std::vector<int>Split(intu){if(col_[u]!=2)return{u};std::vector<int>ng;if(typ_[u]){// Qif(!Check(g_[u])){std::reverse(g_[u].begin(),g_[u].end());if(!Check(g_[u]))Fail();}for(auto&&v:g_[u])if(col_[v]!=2){ng.emplace_back(v);}else{autos=Split(v);ng.insert(ng.end(),s.begin(),s.end());}}else{// Pstd::vector<int>son[3];for(auto&&x:g_[u])son[col_[x]].emplace_back(x);if(son[2].size()>1)Fail();if(!son[0].empty()){intn0=NewNode(0);g_[n0]=son[0];ng.emplace_back(n0);}if(!son[2].empty()){autos=Split(son[2][0]);ng.insert(ng.end(),s.begin(),s.end());}if(!son[1].empty()){intn1=NewNode(0);g_[n1]=son[1];ng.emplace_back(n1);}}Delete(u);returnng;}voidWork(intu){if(col_[u]!=2)return;if(typ_[u]){// Qintl=1e9,r=-1e9;for(inti=0;i<static_cast<int>(g_[u].size());i++)if(col_[g_[u][i]])checkmin(l,i),checkmax(r,i);for(inti=l+1;i<r;i++)if(col_[g_[u][i]]!=1)Fail();if(l==r&&col_[g_[u][l]]==2){Work(g_[u][l]);return;}std::vector<int>ng;for(inti=0;i<l;i++)ng.emplace_back(g_[u][i]);autos=Split(g_[u][l]);ng.insert(ng.end(),s.begin(),s.end());for(inti=l+1;i<r;i++)ng.emplace_back(g_[u][i]);if(l!=r){s=Split(g_[u][r]);std::reverse(s.begin(),s.end());ng.insert(ng.end(),s.begin(),s.end());}for(inti=r+1;i<static_cast<int>(g_[u].size());i++)ng.emplace_back(g_[u][i]);g_[u]=ng;}else{// Pstd::vector<int>son[3];for(auto&&x:g_[u])son[col_[x]].emplace_back(x);if(son[1].empty()&&son[2].size()==1){Work(son[2][0]);return;}g_[u].clear();if(son[2].size()>2)Fail();g_[u]=son[0];intn1=NewNode(1);g_[u].emplace_back(n1);if(son[2].size()>=1){autos=Split(son[2][0]);g_[n1].insert(g_[n1].end(),s.begin(),s.end());}if(son[1].size()){intn2=NewNode(0);g_[n1].emplace_back(n2);g_[n2]=son[1];}if(son[2].size()>=2){autos=Split(son[2][1]);std::reverse(s.begin(),s.end());g_[n1].insert(g_[n1].end(),s.begin(),s.end());}}}voidRemove(intu){// remove the nodes with only one childfor(auto&&v:g_[u]){inttv=v;while(g_[tv].size()==1){intt=tv;tv=g_[tv][0];Delete(t);}v=tv,Remove(v);}}voidDfsAns(intu){if(u>=1&&u<=n_){ans_.emplace_back(u);return;}for(auto&&v:g_[u])DfsAns(v);}}T;