基于BFS的拓扑排序
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n,m;
int q[N],d[N];
struct Node {
int id;
Node* next;
Node(int _id):id(_id),next(NULL) {}
} *head[N];
void add(int a,int b) {
auto p = new Node(b);
p->next = head[a];
head[a] = p;
}
bool topsort() {
int hh = 0,tt = -1;
for(int i = 1;i <= n;i ++) {
if(!d[i]) q[++ tt] = i;
}
while(hh <= tt) {
int t = q[hh ++];
for(auto p = head[t];p;p = p->next) {
d[p->id] --;
if(!d[p->id]) q[++ tt] = p->id;
}
}
return tt == n - 1;
}
int main()
{
cin >> n >> m;
while(m --) {
int a,b;
cin >> a >> b;
add(a,b);
d[b] ++;
}
if(!topsort()) puts("-1");
else {
for(int i = 0;i < n;i ++)
printf("%d ",q[i]);
}
return 0;
}
基于dfs的拓扑排序-记BFS版本拓扑排序即可,这个太难了
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010, M = 100010;
int n, m;
struct Node
{
int id;
Node* next;
Node(int _id): id(_id), next(NULL) {}
}*head[N];
int st[N], q[N], top;
void add(int a, int b)
{
auto p = new Node(b);
p->next = head[a];
head[a] = p;
}
bool dfs(int u)
{
st[u] = 1;
for (auto p = head[u]; p; p = p->next)
{
int j = p->id;
if (!st[j])
{
if (!dfs(j)) return false;
}
else if (st[j] == 1) return false;
}
q[top ++ ] = u;
st[u] = 2;
return true;
}
bool topsort()
{
for (int i = 1; i <= n; i ++ )
if (!st[i] && !dfs(i))
return false;
return true;
}
int main()
{
scanf("%d%d", &n, &m);
while (m -- )
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
if (!topsort()) puts("-1");
else
{
for (int i = n - 1; i >= 0; i -- )
printf("%d ", q[i]);
}
return 0;
}