# Training 8 - B题

## Output

4 3
1 2
2 3
4 3

### Sample Output

1 2 4 3

#pragma warning (disable:4996)
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 500 + 5;
const int M = 1000 + 5;
const int maxn = 10000 + 5;

int head[N], ver[M], nex[N];
int deg[N];//入度
int a[N];//top序列
int tot;
int cnt;

int n, m;

void add(int x, int y)
{
++tot;
ver[tot] = y;
deg[y]++;
}

void topsort()
{
priority_queue<int> q;
for (int i = 1; i <= n; i++)
if (deg[i] == 0)
q.push(-i);
while (q.size())
{
int x = -q.top();
q.pop();
a[++cnt] = x;
for (int i = head[x]; i; i = nex[i])
{
int y = ver[i];
if (--deg[y] == 0)
q.push(-y);
}
}
}

int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
memset(ver, 0, sizeof(ver));
memset(nex, 0, sizeof(nex));
memset(deg, 0, sizeof(deg));
tot = cnt = 0;
for (int i = 1; i <= m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
}
topsort();
for (int i = 1; i <= cnt; i++)
if (i < cnt)
printf("%d ", a[i]);
else
printf("%d\n", a[i]);
}
return 0;
}

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客