有向图的拓扑排序
Method
Example
给定一个n个点m条边的有向图,图中可能存在重边和自环。
请输出任意一个该有向图的拓扑序列,如果拓扑序列不存在,则输出-1。
若一个由图中所有点构成的序列A满足:对于图中的每条边(x, y),x在A中都出现在y之前,则称A是该图的一个拓扑序列。
输入格式
第一行包含两个整数n和m
接下来m行,每行包含两个整数x和y,表示存在一条从点x到点y的有向边(x, y)。
输出格式
共一行,如果存在拓扑序列,则输出拓扑序列。
否则输出-1。
数据范围
1≤n,m≤1051≤n,m≤105
输入样例:
3 3
1 2
2 3
1 3
输出样例:
1 2 3
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, m;
int h[N], e[N], ne[N], idx;
int q[N], d[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
bool topsort()
{
int hh = 0, tt = -1;
//d[i] 存储点i的入度
for (int i = 1; i <= n; i ++)
if (!d[i])
q[++ tt] = i;//入队
while (hh <= tt)
{
int t = q[hh ++];
//枚举t的所有出边
for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
d[j] --;//删除j
if (d[j] == 0) q[++ tt] = j;//删除完将j插入队列
}
}
return tt == n - 1;
}
int main()
{
cin >> n >> m;
memset(h, -1, sizeof h);
for (int i = 0; i < m; i ++)
{
int a, b;
cin >> a >> b;
add(a, b);
d[b] ++;
}
if (topsort())
{
for (int i = 0; i < n; i ++) printf("%d ", q[i]);
puts("");
}
else puts("-1");
return 0;
}