#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<stack>
using namespace std;
map<int,int> int2int;
void init(int t,stack<int> st[])
{
while(1)
{
int index=int2int[t];
int temp=st[index].top();
if(temp==t)
return;
else
{
st[temp].push(temp);
int2int[temp]=temp;
st[index].pop();
}
}
}
void pile(int a,int b,stack<int> st[])
{
int index1=int2int[a];
stack<int> temp;
while(1)
{
int t=st[index1].top();
st[index1].pop();
temp.push(t);
if(t==a)
break;
}
int index2=int2int[b];
while(temp.size()>0)
{
int t=temp.top();
temp.pop();
st[index2].push(t);
int2int[t]=index2;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
stack<int> st[25];
int i,j;
for(i=0;i<n;i++)
{
st[i].push(i);
int2int[i]=i;
}
string s;
while(cin>>s)
{
if(s[0]=='q')
break;
string s1;
int a,b;
cin>>a>>s1>>b;
if(int2int[a]==int2int[b])
continue;
if(s[0]=='m')
{
init(a,st);
}
if(s1[1]=='n')
{
init(b,st);
}
pile(a,b,st);
}
for(i=0;i<n;i++)
{
printf("%d:",i);
stack<int> out;
while(st[i].size()>0)
{
int t=st[i].top();
st[i].pop();
out.push(t);
}
while(out.size()>0)
{
int t=out.top();
out.pop();
printf(" %d",t);
}
printf("\n");
}
}
return 0;
}
Uva 101 The blocks problem
最新推荐文章于 2024-02-26 12:33:46 发布