#define _CRT_SECURE_NO_WARNINGS
//#define MAX 1001
#define MAX 10
#include <stdio.h>
typedef struct vertex {
int num; //정점번호
struct vertex* next;//다음노드
}Vertex;
Vertex* arr[MAX];
int V, E;
int preEdge[MAX];
bool check[MAX];
void insertEdge(int a, int b)
{
Vertex* newVertex = new Vertex;
newVertex->num = b;
newVertex->next = NULL;
if (arr[a] == NULL)
arr[a] = newVertex;
else {
newVertex->next = arr[a];
arr[a] = newVertex;
}
}
void sol(int n)
{
Vertex* cur = arr[n];
while (cur != NULL)
{
preEdge[cur->num]--;
if (!check[cur->num] && preEdge[cur->num] == 0) {
printf(" %d", cur->num);
check[cur->num] = true;
preEdge[cur->num]--;
sol(cur->num);
}
cur = cur->next;
}
}
int main()
{
freopen("input.txt","r",stdin);
for (int t = 1; t <= 10; t++)
{
scanf("%d%d", &V, &E);
for (int i = 0; i<E; i++)
{
int a, b;
scanf("%d%d", &a, &b);
insertEdge(a, b);
preEdge[b]++;
}
printf("#%d", t);
for (int i = 1; i <= V; i++)
{
if (preEdge[i] == 0) {
printf(" %d", i);
sol(i);
}
}
printf("\n");
for (int i = 1; i <= V; i++)
{
check[i] = false;
preEdge[i] = 0;
arr[i] = NULL;
}
}
}
댓글