UVA 540

#include<iostream>
#include<queue>
#include<map>
#include<cstring>
using namespace std;
const int maxn=1000+10;
int main()
{
  int t,kase=0,x;
  while(scanf("%d",&t)==1&&t)
  {
    printf("Scenario #%d\n",++kase);
    map<int,int>team;
    for(int i=0;i<t;i++)
    {
      int n;
      cin>>n;
      for(int j=0;j<n;j++)
      {
        cin>>x;
        team[x]=i; //表示编号为x的 所在的团队。
      }
    }
    queue<int>q,q2[maxn]; //q是整个队列,q2[i]是团队i 成员的队列。
    while(1)
    {
      char cmd[10];
      scanf("%s",cmd);
      if(cmd[0]=='S')
        break;
      else if(cmd[0]=='D')
      {
        int p=q.front(); //队首出队
        printf("%d\n",q2[p].front()); //也就意味着这个团队中的队首出队
          q2[p].pop();
        if(q2[p].empty())//如果这个团队队列不空的话,整个队列也要清除一次。
          q.pop();
      }
      else if(cmd[0]=='E')
      {
        cin>>x;
        int k=team[x];//获取x所在团队的编号
        if(q2[k].empty()) q.push(k); //42行月43行代码不能调换,因为当判断队列为空时,先放入整个队列中,再放入团队队列中。若不是直接放入团队队列中。
        q2[k].push(x);
      }
    }
  printf("\n");
}
return 0;
}

posted @ 2019-05-27 19:43  ACM-Lxw  阅读(175)  评论(0编辑  收藏  举报