#include <stdio.h>
struct NODE
{
int n;
NODE* next;
};
const int MAXN = 6, MAXM = 6;
//const int MAXN = 50000, MAXM = 150000;
NODE* list[MAXN + 1];
NODE nPool[MAXM];
int CHK[MAXN + 1];
int QUEUE[MAXN];
int wp, rp;
int tc, T, N, M, a, b, s;
void process(void)
{
wp = 0, rp = 0;
for (int i = 1; i <= N; ++i)
{
if (CHK[i] == 0) QUEUE[wp++] = i;
}
while (rp < wp)
{
s = QUEUE[rp++];
for (NODE* n = list[s]; n != NULL; n = n->next)
{
if (--CHK[n->n] == 0) QUEUE[wp++] = n->n;
}
}
}
int main(void)
{
freopen("input.txt", "r", stdin);
for (tc = scanf("%d", &T); tc <= T; ++tc)
{
scanf("%d%d", &N, &M);
for (int i = 0; i < M; ++i)
{
scanf("%d%d", &a, &b);
NODE* n = &nPool[i];
n->n = b, n->next = list[a], list[a] = n;
++CHK[b];
}
printf("#%d", tc);
process();
for (int i = 0; i < wp; ++i) printf(" %d", QUEUE[i]);
for (int i = 1; i <= N; ++i) CHK[i] = 0, list[i] = NULL;
puts("");
}
return 0;
}
댓글