#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAXN 1000000#define LOCALtypedefstructnode{
structnode*next;
intdata;
} node;
typedefstructteam_node{
node*head;
node*rear;
structteam_node*next;
}team_node;
intteam[MAXN];
intnum_team;
voidinit(team_node**head);
voidenqueue(team_node**head, team_node**tail, intelement);
intsearch_team(int*team, intelement);
intdequeue(team_node**head);
intmain()
{
team_node*head=NULL;
team_node*tail=NULL;
intnum;
intno;
inti, j;
charbuf[10];
intelement;
inttests=0;
#ifndef LOCALfreopen("c://uva_in.txt", "r", stdin);
#endifwhile(scanf("%d", &num_team) &&num_team!=0)
{
memset(team, -1, sizeof(team));
for(i=0; i<num_team; i++)
{
scanf("%d", &num);
for(j=0; j<num; j++)
{
scanf("%d", &no);
team[no] =i;
}
}
head= (team_node*)malloc(sizeof(structteam_node));
init(&head);
tail=NULL;
printf("Scenario #%d/n", ++tests);
while(scanf("%s", buf) &&strcmp(buf, "STOP") !=0)
{
if(strcmp(buf,"ENQUEUE") ==0)
{
scanf("%d", &element);
enqueue(&head, &tail, element);
}
else {
element=dequeue(&head);
printf("%d/n", element);
}
}
printf("\n");
}
return0;
}
voidinit(team_node**head)
{
(*head)->next=NULL;
(*head)->head= (*head)->rear=NULL;
}
voidenqueue(team_node**head, team_node**tail, intelement)
{
team_node*p;
node*q;
intteam1, team2;
intdata;
intflag;
if ((*head)->next==NULL)
{
q= (node*)malloc(sizeof(structnode));
q->data=element;
q->next=NULL;
p= (team_node*)malloc(sizeof(structteam_node));
p->next=NULL;
p->head=p->rear= (node*)malloc(sizeof(structnode));
p->rear->next=q;
p->rear=q;
(*head)->next=p;
*tail=p;
}
else {
flag=0p= (*head)->next;
while(p)
{
data=p->head->next->data;
team1=search_team(team, data);
team2=search_team(team, element);
if(team1==team2)
{
q= (node*)malloc(sizeof(structnode));
q->data=element;
q->next=NULL;
p->rear->next=q;
p->rear=q;
flag=1;
break;
}
elsep=p->next;
}
if (!flag)
{
q= (node*)malloc(sizeof(structnode));
q->data=element;
q->next=NULL;
p= (team_node*)malloc(sizeof(structteam_node));
p->next=NULL;
p->head=p->rear= (node*)malloc(sizeof(structnode));
p->rear->next=q;
p->rear=q;
(*tail)->next=p;
*tail=p;
}
}
}
intsearch_team(int*team, intelement)
{
returnteam[element];
}
intdequeue(team_node**head)
{
team_node*p;
node*q;
intdata;
p= (*head)->next;
q=p->head->next;
data=q->data;
p->head->next=q->next;
free(q);
if(p->rear==q)
{
p->rear=p->head;
(*head)->next=p->next;
free(p->rear);
free(p);
}
returndata;
}