本来想开个二维数组,担心数据坑人越界,就用指针写了,开了个指针数组来存放每个队的末尾元素的指针。
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node{
node *next;
int n;
};
int belong[1000005];
node *team[1005];
node *head,*tail;
void add(){
int m; cin>>m;
int t=belong[m];
node *p=new node; p->n=m;
if(team[t]==NULL){
if(head==NULL) head=p;
else tail->next=p;
p->next=NULL; tail=p;
}
else{
p->next=team[t]->next;
team[t]->next=p;
if(team[t]==tail) tail=p;
}
team[t]=p;
}
void prt(){
cout<<head->n<<endl;
node *p=head;
int t=belong[head->n];
if( head==team[t] ) team[t]=NULL;
head=head->next; delete p;
}
int main()
{
int c=1,t;
while(cin>>t){
if(t==0) break;
printf("Scenario #%d\n",c++);
memset(team,0,sizeof(team)); head=tail=NULL;
while(t--){
int k,a; cin>>k;
while(k--){ cin>>a; belong[a]=t; }
}
char s[20];
while(1){
cin>>s; if(s[0]=='S') { cout<<endl; break; }
if(s[0]=='E') add();
else prt();
}
}
return 0;
}