#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
n;
int
m;
int
*in;
int
cnt;
char
*check;
typedef
struct
Node
{
char
data;
struct
Node *next;
}Node;
typedef
struct
{
Node *front;
Node *rear;
int
count;
}Queue;
Queue queue;
void
InitQueue(Queue *queue)
{
queue->front = queue->rear = NULL;
queue->count = 0;
}
int
IsEmpty(Queue *queue)
{
return
queue->count == 0;
}
void
Enqueue(Queue *queue,
char
data)
{
Node *now = (Node *)
malloc
(
sizeof
(Node));
now->data = data;
now->next = NULL;
if
(IsEmpty(queue))
{
queue->front = now;
}
else
{
queue->rear->next = now;
}
queue->rear = now;
queue->count++;
}
int
Dequeue(Queue *queue)
{
char
re = 0;
Node *now;
if
(IsEmpty(queue))
{
return
0;
}
now = queue->front;
re = now->data;
queue->front = now->next;
free
(now);
queue->count--;
return
re;
}
typedef
struct
{
char
vertex;
int
indegree;
struct
node *link;
}node;
node **g;
void
initializeGraph()
{
g = (node**)
malloc
(
sizeof
(node*)*(n + 1));
for
(
int
i = 0; i <= n; i++)
{
g[i] = (node*)
malloc
(
sizeof
(node));
g[i]->link = NULL;
in[i] = 0;
}
}
void
insertVertex(
char
vName,
int
i)
{
g[i]->vertex = vName;
g[i]->indegree = 0;
}
void
insertDirectedEdge(
char
uName,
char
wName)
{
node *newnode;
node *cur;
newnode = (node*)
malloc
(
sizeof
(node));
newnode->vertex = wName;
newnode->link = NULL;
cur = NULL;
for
(
int
i = 0; i < n; i++)
{
if
(g[i]->vertex == uName)
{
cur = g[i];
break
;
}
}
newnode->link = cur->link;
cur->link = newnode;
for
(
int
i = 0; i < n; i++)
{
if
(g[i]->vertex == wName)
{
g[i]->indegree++;
}
}
}
void
buildGraph()
{
initializeGraph();
char
vName;
char
uName;
char
wName;
getchar
();
for
(
int
i = 0; i < n; i++)
{
scanf
(
"%c"
, &vName);
insertVertex(vName, i);
getchar
();
}
scanf
(
"%d"
, &m);
getchar
();
for
(
int
i = 0; i < m; i++)
{
scanf
(
"%c %c"
, &uName, &wName);
insertDirectedEdge(uName, wName);
getchar
();
}
}
int
index(
char
Name)
{
for
(
int
i = 0; i < n; i++)
{
if
(g[i]->vertex == Name)
{
return
i;
}
}
}
int
topologicalSort()
{
node *cur;
char
Name;
int
j = 0;
int
flag = 0;
int
flag2 = 0;
cnt = 0;
InitQueue(&queue);
cur = (Node*)
malloc
(
sizeof
(Node));
cur->link = NULL;
for
(
int
i = 0; i < n; i++)
{
in[i] = g[i]->vertex;
if
(g[i]->indegree == 0)
{
Enqueue(&queue,g[i]->vertex);
check[cnt++] = g[i]->vertex;
}
}
while
(1)
{
Name = Dequeue(&queue);
j = index(Name);
cur = g[j]->link;
if
(n == cnt)
break
;
while
(cur != NULL)
{
for
(
int
i = 0; i < n; i++)
{
if
(cur->vertex == g[i]->vertex)
{
g[i]->indegree--;
}
}
cur = cur->link;
}
for
(
int
i = n; i >= 0; i--)
{
if
(g[i]->indegree == 0)
{
for
(
int
k = cnt; k >= 0; k--) {
if
(g[i]->vertex == check[k])
{
flag2 = 1;
break
;
}
}
if
(flag2 == 0)
{
Enqueue(&queue, g[i]->vertex);
check[cnt++] = g[i]->vertex;
}
else
flag2 = 0;
}
}
if
(IsEmpty(&queue) != 0 ) {
flag = 1;
break
;
}
}
if
(flag == 1)
return
0;
else
return
1;
}
void
main(
void
)
{
int
finish;
char
c;
node *cur;
InitQueue(&queue);
scanf
(
"%d"
, &n);
in = (
int
*)
malloc
(
sizeof
(
int
)*(n + 1));
check = (
char
*)
malloc
(
sizeof
(
char
)*(n + 1));
buildGraph();
finish = topologicalSort();
if
(finish == 0)
printf
(
"0"
);
else
{
for
(
int
i = 0; i < cnt; i++)
{
printf
(
"%c "
, check[i]);
}
}
free
(in);
free
(check);
}
</stdlib.h></string.h></stdio.h>