首页 > 其他分享 >拓扑排序

拓扑排序

时间:2022-12-21 12:22:33浏览次数:64  
标签:int inDegree 拓扑 nextInt static new 排序 adj

拓扑排序

TODO

待补全

Code

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

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

    static List<Integer>[] adj;
    static int n, m;

    static int[] getInDegree() {
        int[] inDegree = new int[n];
        for (List<Integer> list : adj) {
            for (Integer to : list) {
                ++inDegree[to];
            }
        }
        return inDegree;
    }

    static boolean topologicalSorting() {
        Stack<Integer> S = new Stack<Integer>();
        int[] inDegree = getInDegree();
        for (int i = 0; i < n; ++i) {
            if (inDegree[i] == 0) {
                S.push(i);
            }
        }
        int cnt = 0;
        while (!S.isEmpty()) {
            ++cnt;
            for (Integer to : adj[S.pop()]) {
                if (--inDegree[to] == 0) S.push(to);
            }
        }
        return cnt == n;
    }

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        adj = new LinkedList[n];
        for (int i = 0; i < n; ++i) adj[i] = new LinkedList<Integer>();
        for (int i = 0; i < m; ++i) adj[nextInt()].add(nextInt());
        topologicalSorting();
    }
}

测试数据

13 15
8 7
7 6
6 9
9 10
9 11
9 12
11 12
6 4
0 6
5 4
0 5
0 1
2 0
2 3
3 5

参考资料

拓扑排序 - OI Wiki

标签:int,inDegree,拓扑,nextInt,static,new,排序,adj
From: https://www.cnblogs.com/Cattle-Horse/p/16995991.html

相关文章

  • MySQL高可用工具Orchestrator:复制拓扑的发现
    1、orchestrator如何去发现mysql实例这个涉及到两个参数:HostnameResolveMethod和MySQLHostnameResolveMethodHostnameResolveMethod的值有三个选项:  "cname":通过c......
  • [vue] 列表排序
    <divid="root"><h2>人员列表</h2><inputtype="text"placeholder="请输入名字"v-model="keyWord"><button@click="sortType=2">年龄升序</button><button@cl......
  • 图解排序算法,这五种最热门!
    介绍5种热门的排序算法,用图文+代码的方式讲解~说到排序算法,大家估计都比较熟悉,但要你一下子写出来又蒙圈了。所以这篇文章不会讲解所有的排序算......
  • C#-记录几个排序算法(选择/插入/冒泡/希尔)
     给定一组数据,使用不同的算法对其进行递增排序:int[]rawList={12,33,21,2,43,5,67,8,96,4,22,36,23,42,90};选择排序:找到最大的数值,交换在最后一......
  • 数据结构(起泡排序)
    #include<stdio.h>#defineKEYTYPEint#defineMAXSIZE100typedefstruct{KEYTYPEkey;}SSELEMENT;intCOMPARE(SSELEMENTa[],intn){inti,j,p,flag;i=n......
  • AcWing787.归并排序
    题目描述给定你一个长度为\(n\)的整数数列。请你使用归并排序对这个数列按照从小到大进行排序。并将排好序的数列按顺序输出。输入格式输入共两行,第一行包含整数\(......
  • 技术分享 | InnoDB 排序索引的构建
    作者:SatyaBodapati翻译:管长龙从MySQL5.7开始,开发人员改变了InnoDB构建二级索引的方式,采用自下而上的方法,而不是早期版本中自上而下的方法了。在这篇文章中,我们将通过......
  • 数据结构堆(Heap)&排序
    在我们描述堆之前,我们首先要明白一个概念,什么是树?树的概念及结构1.树的概念树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合。把它叫做树是......
  • 基础算法(排序 & 查找)
    快速排序、归并排序、整数二分查找、浮点数二分查找快速排序主要思想是分治:确定分界点调整范围递归处理左右两段代码:#include<iostream>usingnamespacestd;......
  • [数据分析与可视化] 数据绘图要点1_注重数据排序
    date:2021-11-1410:45:06+0800tags:-数据分析与可视化-R语言数据绘图要点1-注重数据排序默认情况下,大多数数据可视化工具将使用字母顺序或使用输入表中的出......