#include<iostream>
#include<string>
#include<cstdio>
#include<stack>
using namespace std;
stack<int> stk[30],ss;
string s1,s2;
int k,kk,n,father[30];
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++)
stk[i].push(i),father[i]=i;
while(1){
cin>>s1;
if(s1=="quit")
break;
cin>>k>>s2>>kk;
int a=father[k],b=father[kk];
if(a==b)
continue;
if(s1=="move"){
if(s2=="onto"){
while(!stk[b].empty()){
int f=stk[b].top();
if(f==kk)
break;
stk[b].pop();
stk[f].push(f);
father[f]=f;
}
while(!stk[a].empty()){
int f=stk[a].top();
if(f==k)
break;
stk[a].pop();
stk[f].push(f);
father[f]=f;
}
stk[a].pop();
stk[b].push(k);
father[k]=b;
} else{
while(!stk[a].empty()){
int f=stk[a].top();
if(f==k)
break;
stk[a].pop();
stk[f].push(f);
father[f]=f;
}
stk[a].pop();
stk[b].push(a);
father[k]=b;
}
} else{
if(s2=="onto"){
while(!stk[b].empty()){
int f=stk[b].top();
if(f==kk)
break;
stk[b].pop();
stk[f].push(f);
father[f]=f;
}
while(!stk[a].empty()){
int f=stk[a].top();
father[f]=b;
if(f==k)
break;
stk[a].pop();
ss.push(f);
}
ss.push(k);
stk[a].pop();
while(!ss.empty()){
stk[b].push(ss.top());
ss.pop();
}
} else{
while(!stk[a].empty()){
int f=stk[a].top();
father[f]=b;
if(f==k)
break;
stk[a].pop();
ss.push(f);
}
ss.push(k);
stk[a].pop();
while(!ss.empty()){
stk[b].push(ss.top());
ss.pop();
}
}
}
}
for(int i=0;i<n;i++){
printf("%d:",i);
while(!stk[i].empty()){
ss.push(stk[i].top());
stk[i].pop();
}
while(!ss.empty()){
printf(" %d",ss.top());
ss.pop();
}
if(i!=n-1)
puts("");
}
return 0;
}
在POJ上面A了,这里时限3倍怎么还T啊?