给定一个 n 个点 m 条边的有向图,点的编号是 1 到 n,图中可能存在重边和自环。
请输出任意一个该有向图的拓扑序列,如果拓扑序列不存在,则输出 −1。
若一个由图中所有点构成的序列 A 满足:对于图中的每条边 (x,y),x 在 A 中都出现在 y 之前,则称 A 是该图的一个拓扑序列。
输入格式
第一行包含两个整数 n 和 m。
接下来 m 行,每行包含两个整数 x 和 y,表示存在一条从点 x 到点 y 的有向边 (x,y)。
输出格式
共一行,如果存在拓扑序列,则输出任意一个合法的拓扑序列即可。
否则输出 −1。
数据范围
1≤n,m≤10^5
输入样例:
3 3
1 2
2 3
1 3
输出样例:
1 2 3
- 用数组模拟队列,队列出队的顺序就是有向无环图的拓扑序(注意:拓扑序列是不唯一的)
import java.io.*;
import java.util.Arrays;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int n, m, idx = 0;
static class Edge {
private int v, ne;
public Edge(int v, int ne) {
this.v = v;
this.ne = ne;
}
}
static Edge[] edges;
static int[] h, d, q;
public static void main(String[] args) throws IOException {
String[] line = br.readLine().split(" ");
n = Integer.parseInt(line[0]);
m = Integer.parseInt(line[1]);
edges = new Edge[m + 1];
h = new int[n + 1];
d = new int[n + 1];
q = new int[n + 1];
Arrays.fill(h, -1);
while (m-- > 0) {
line = br.readLine().split(" ");
int u = Integer.parseInt(line[0]), v = Integer.parseInt(line[1]);
add(u, v);
d[v]++;
}
if (topSort()) {
for (int i = 0; i < n; i++) {
out.print(q[i] + " ");
}
}else out.println("-1");
br.close();
out.close();
}
public static void add(int u, int v) {
edges[++idx] = new Edge(v, h[u]);
h[u] = idx;
}
public static boolean topSort() {
int head = 0, tail = -1;
for (int i = 1; i <= n; i++) {
if (d[i] == 0) q[++tail] = i;
}
while (head <= tail) {
int t = q[head++];
for (int i = h[t]; i != -1; i = edges[i].ne) {
int v = edges[i].v;
d[v]--;
if (d[v] == 0) q[++tail] = v;
}
}
return tail == n - 1;
}
}
- Queue + ArrayList存拓扑序列
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int n, m, idx = 0;
static class Edge {
private int v, ne;
public Edge(int v, int ne) {
this.v = v;
this.ne = ne;
}
}
static Edge[] edges;
static int[] h, d;
static List<Integer> res;
public static void main(String[] args) throws IOException {
String[] line = br.readLine().split(" ");
n = Integer.parseInt(line[0]);
m = Integer.parseInt(line[1]);
edges = new Edge[m + 1];
h = new int[n + 1];
d = new int[n + 1];
res = new ArrayList<>(n);
Arrays.fill(h, -1);
while (m-- > 0) {
line = br.readLine().split(" ");
int u = Integer.parseInt(line[0]), v = Integer.parseInt(line[1]);
add(u, v);
d[v]++;
}
topSort();
if (res.size() == n) {
for (int x : res) out.print(x + " ");
}else out.println("-1");
br.close();
out.close();
}
public static void add(int u, int v) {
edges[++idx] = new Edge(v, h[u]);
h[u] = idx;
}
public static void topSort() {
Queue<Integer> q = new LinkedList<>();
for (int i = 1; i <= n; i++) {
if (d[i] == 0) q.offer(i);
}
while (!q.isEmpty()) {
int t = q.poll();
res.add(t);
for (int i = h[t]; i != -1; i = edges[i].ne) {
int v = edges[i].v;
d[v]--;
if (d[v] == 0) q.offer(v);
}
}
}
}