首页 > 其他分享 >【学习】拓扑排序

【学习】拓扑排序

时间:2023-08-03 17:57:51浏览次数:29  
标签:head int 拓扑 tot 学习 排序 void

拓扑排序学习笔记

忘了学没学过了,就当没学过吧

推歌:Oliver《D.S.》

B 站以外好像没有能听的

概念

拓扑排序的要求:有向无环图(TAG图)。

拓扑序列中,一条有向边的起点一定排在它的重点的前面。

由此可得拓扑序列求法:每次找到入度为 \(0\) 的点,把它加入序列中;删除它和由它出发的边,然后重复以上操作。

实现

Kahn 算法是一种 \(O(n+m)\) 复杂度实现上述过程的算法。

它的中心思想是用队列存下当前入度为 \(0\) 的点,然后由队首的点进行遍历。

展开代码
//top数组也可以考虑使用动态数组
void topsort() {
    tot = 0;
    queue<int> q;
    for(int i = 1; i <= n; ++i) if(!ind[i]) q.push(i);
    while(!q.empty()) {
        int ft = q.front();
        q.pop();
        top[++tot] = ft;
        for(int i = head[ft]; i; i = nxt[i]) {
            int x = to[i];
            --ind[x];
            if(!ind[x]) q.push(x);
        }
    }
}

例题

B3644 家谱树

这题什么时候到 B 题库了

简单模板,注意处理输入:

接下来 \(N\) 行,第 \(i\) 行描述第 \(i\) 个人的后代编号 \(a_{i,j}\), 表示 \(a_{i,j}\) 是 \(i\) 的后代。每行最后是 \(0\) 表示描述完毕。

以及别犯低级错误:

展开代码
#include<bits/stdc++.h>
using namespace std;
const int N = 205;
int n, a, head[N], to[N], nxt[N], ind[N], top[N], tot;
void add(int u, int v) {
    to[++tot] = v;
    nxt[tot] = head[u];
    head[u] = tot;
    ++ind[v];
}
void topsort() {
    tot = 0;
    queue<int> q;
    for(int i = 1; i <= n; ++i) if(!ind[i]) q.push(i);
    while(!q.empty()) {
        int ft = q.front();
        q.pop();
        top[++tot] = ft;
        for(int i = head[ft]; i; i = nxt[i]) {
            int x = to[i];
            --ind[x];
            if(!ind[x]) q.push(x);
        }
    }
}
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        while(1){
            scanf("%d", &a);
            if(!a) break;
            add(i, a);
        }
    }
    topsort();
    for(int i = 1; i <= n; ++i) printf("%d ", top[i]);
	return 0;
}

标签:head,int,拓扑,tot,学习,排序,void
From: https://www.cnblogs.com/Kiichi/p/topsortnote.html

相关文章

  • 【SpringBoot学习】5、SpringBoot 实现文件上传,图片上传并显示功能
    SpringBoot实现文件上传,图片上传并显示功能我这里使用的是springboot2.0.3,不需要导入相关jar包,2.x的版本已经整合进去了,直接使用即可。spring官网提供了springboot的文件上传下载案例,这是网址:https://spring.io/guides/gs/uploading-files/,使用的是流的输出。下面的案......
  • Asp.net MVC 3实例学习之ExtShop(一)————创建应用并设置开发环境
         在VS2010中创建一个如图1所示的“ExtShop”项目,然后在图2的窗口中选择“Empty”,单击“OK”完成项目创建,项目的目录结构和已包含文件如图3所示。     其中,Content文件夹下的Site.css文件是整个网站的CSS文件。Script文件夹中,已包含了jquery的脚本文件。在View目录......
  • three.js学习2-性能监测工具stats.js
    1.安装npmistats.js2.组件引入import*asStatsfrom'stats.js'3.使用,requestAnimationFrame循环调用的函数中调用方法update(),来刷新时间//创建性能检测letstats=newStats()stats.showPanel(0)//0:fps,1:ms,2:mb,3+:customdocument.body.appe......
  • 【SpringBoot学习】4、SpringBoot 配置本地资源映射路径已解决
    springboot配置本地资源映射路径需要配置一下映射资源位置,当时springboot1.x和spring波特2.x的配置方法不同,这里就分开记录一下配置过程。1、springboot1.x配置@ConfigurationpublicclassMyWebMvcConfigurerAdapterextendsWebMvcConfigurerAdapter{@Overri......
  • nginx学习---初识nginx
    1.Nginx知识网结构图Nginx是一个高性能的HTTP和反向代理服务器,特点是占用内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好nginx专为性能优化而开发,性能是其最重要的要求,十分注重效率,有报告nginx能支持高达50000个并发连接数1.1反向代理正向代理:局......
  • Java(从零到企业级电商项目实战)学习笔记
    资料网站:http://learning.happymmall.com/env.html一、mybatis三剑客:generator,plugin,pagehelperpagehelper->https://github.com/pagehelper/Mybatis-PageHelper二、spring例子:https://github.com/spring-projects/spring-mvc-showcasehttps://github.com/spring-proj......
  • 【SpringBoot学习】3、SpringBoot 多个版本配置简单的拦截器
    springboot1.x和springboot2.x配置拦截器区别就在于注册拦截器的方式不同,springboot1.x配置方法是:publicclassWebAppConfigextendsWebMvcConfigurerAdapter{springboot2.x配置方法是:publicclassLoginConfigurerimplementsWebMvcConfigurer{下面详细的介绍使......
  • [算法学习笔记] 多重背包--二进制拆分
    多重背包回顾一下多重背包是什么?有\(n\)种物品,每个物品都有有限个,每个物品都有重量和价值两个参数,你有一个限重为\(W\)的背包,求背包内价值最大。我们朴素的做法是将多重背包拆分成01背包求解,因为每个物品都有有限个,假设第\(i\)个物品有\(j\)个,那么跑\(j\)次01背包即可。但是这......
  • 【SpringBoot学习】2、idea 配置 SpringBoot 热启动详解,和热启动失效解决方案
    一、idea配置springboot热启动方法1、添加spring-boot-devtools的包,true必须加上。<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></d......
  • 【安全学习之路】Day38
    ......