【模板】拓扑排序

Problem: 【模板】拓扑排序

思路

拓扑排序模板

解题方法

  1. 初始化一个队列,将所有入度为0的顶点入队。
  2. 从队列中取出一个顶点,并将其输出。
  3. 对于该顶点的所有出边,将出边的终点的入度减1。
  4. 如果某个顶点的入度变为0,则将其入队。
  5. 重复步骤2-4,直到队列为空。

复杂度

时间复杂度:

时间复杂度: O ( V + E ) O(V+E) O(V+E),其中V是顶点数,E是边数。

空间复杂度:

空间复杂度: O ( V ) O(V) O(V),其中V是顶点数。

Code

import java.util.*;
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static StreamTokenizer sr = new StreamTokenizer(in);
    public static int MAXN = 200001;

    public static int MAXM = 200001;

    // 链式前向星建图
    public static int[] head = new int[MAXN];
    public static int[] next = new int[MAXM];
    public static int[] to = new int[MAXN];
    public static int cnt;

    public static int[] queue = new int[MAXN];
    public static int n, m, l, r;
    public static int[] ans = new int[MAXN];

    public static int[] indegree = new int[MAXN];
    public static void build(int n) {
        cnt = 1;
        Arrays.fill(head, 0, n + 1, 0);
        Arrays.fill(indegree, 0, n + 1, 0);
    }
    public static void addEdge(int f, int t) {
        next[cnt] = head[f];
        to[cnt] = t;
        head[f] = cnt++;
    }

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        build(n);
        for (int i = 0, from, to; i < m; i++) {
            from = nextInt();
            to = nextInt();
            addEdge(from, to);
            indegree[to]++;
        }
        if (!topoSort()) {
            out.println(-1);
        } else {
            for (int i = 0; i < n - 1; i++) {
                out.print(ans[i] + " ");
            }
            out.println(ans[n - 1]);
        }
        out.flush();

    }
    public static boolean topoSort() {
        l = r = 0;
        for (int i = 1; i <= n; i++) {
            if (indegree[i] == 0) {
                queue[r++] = i;
            }
        }
        int fill = 0;
        while(l < r) {
            int cur = queue[l++];
            ans[fill++] = cur;
            for(int ei = head[cur]; ei > 0; ei = next[ei]) {
                if(--indegree[to[ei]] == 0) {
                    queue[r++] = to[ei];
                }
            } 
        }
        return fill == n;


    }
    static int nextInt() throws IOException {
        sr.nextToken();
        return (int)sr.nval;
    }
}

相关推荐

  1. 模板拓扑排序

    2024-01-28 03:04:01       37 阅读
  2. 1191. 家谱树(拓扑排序模板题)

    2024-01-28 03:04:01       39 阅读
  3. 848. 有向图的拓扑序列(拓扑排序模板题)

    2024-01-28 03:04:01       38 阅读
  4. 【算法】拓扑排序

    2024-01-28 03:04:01       18 阅读
  5. 复习拓扑排序

    2024-01-28 03:04:01       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-28 03:04:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-28 03:04:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-28 03:04:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-28 03:04:01       18 阅读

热门阅读

  1. ·迭代器模式

    2024-01-28 03:04:01       29 阅读
  2. 特殊类的设计

    2024-01-28 03:04:01       32 阅读
  3. Canvas图像与视频基础,离屏19

    2024-01-28 03:04:01       29 阅读
  4. KY115 后缀子串排序

    2024-01-28 03:04:01       28 阅读
  5. 【Vue】1-3、Webpack 中的 loader

    2024-01-28 03:04:01       29 阅读
  6. 技术周总结 2024.01.22-01.28

    2024-01-28 03:04:01       39 阅读
  7. 蓝桥杯省一题单

    2024-01-28 03:04:01       34 阅读
  8. python的深浅拷贝

    2024-01-28 03:04:01       33 阅读