• jQuery火箭图标返回顶部代码


    题面

    关于拓扑排序

    因为这好几次考试的题目里都有在DAG中拓扑排序求最长/短路

    txt说它非常的好用

    就找了个题做了下

    拓扑排序就是寻找图中所有的入度为零的点把他入队

    然后再枚举它所有的连到的点,只要去掉它后又是一个入度为零的点就继续入队

    在入队的过程中不断更新最小值

    直至队列为空

    Code:

    #include <queue>
    #include <cstdio>
    #include <iostream>
    using namespace std;
    const int N = 1e5+7;
    queue<int> q;
    int n, m, head[N << 1], dis[N], rd[N];
    struct node {int nxt, to;}e[N << 1];
    int read() {
        int s = 0, w = 1; char ch = getchar();
        while(!isdigit(ch)) {if(ch == '-') w = -1; ch = getchar();}
        while(isdigit(ch)) {s = s * 10 + ch - '0'; ch = getchar();}
        return s * w;
    }
    int main() {
        n = read(), m = read();
        for(int i = 1, x, y; i <= m; i++) 
            x = read(), y = read(), e[i].nxt = head[x], e[i].to = y, head[x] = i, rd[y]++;
        for(int i = 1; i <= n; i++) 
            if(!rd[i]) dis[i] = 1, q.push(i);
        while(!q.empty()) {
            int he = q.front(); q.pop();
            for(int i = head[he]; i; i = e[i].nxt) {
                dis[e[i].to] = max(dis[e[i].to], dis[he] + 1);
                if(!--rd[e[i].to]) q.push(e[i].to);
            }
        }
        for(int i = 1; i <= n; i++) printf("%d
    ", dis[i]);
        return 0;
    } 

    谢谢收看, 祝身体健康!

  • 相关阅读:
    页面定制CSS代码
    记录-20190511
    Java集合
    EL表达式
    javabean
    写一篇博文介绍JSP
    编写一篇博文介绍COOKIE和Session的原理及异同
    过滤器的使用
    Java种的String
    Java包装类
  • 原文地址:https://www.cnblogs.com/yanxiujie/p/11818584.html
Copyright © 2020-2023  润新知