10305 - Ordering Tasks(拓扑排序)

本文介绍了一种经典的拓扑排序算法实现方式,并针对紫书上给出的代码进行了修正。通过使用深度优先搜索(DFS)来完成拓扑排序,确保了在有向无环图(DAG)中节点能够按依赖关系正确排序。

经典的拓扑排序。注意m可以为0。

另外紫书上的代码有错误。。。它的toposort()和dfs()里的u和v都是从0开始的,显然不对。。应该是for(int u=1;u<=n;u++)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100+9;
int c[maxn],topo[maxn],t,n,m,G[maxn][maxn];
bool dfs(int u) {
    c[u] = -1;
    for(int v=1;v<=n;v++)
    if(G[u][v]) {
        if(c[v]<0) return false;
        else if(!c[v]&&!dfs(v)) return false;
    }
    c[u]=1; topo[--t]=u;
    return true;
}
bool toposort() {
    t=n;
    memset(c,0,sizeof(c));
    for(int u=1;u<=n;u++)
    if(!c[u]&&!dfs(u)) return false;
    return true;
}
int main(){
    while(cin>>n>>m){
        if(!n&&!m) break;
        memset(G,0,sizeof(G));
        for(int i=0;i<m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            G[u][v]=1;
        }
        if(toposort())
            for(int i=0;i<n;i++)
            if(i) printf(" %d",topo[i]);
            else printf("%d",topo[i]);
        printf("\n");
    }
    return 0;
}



### Pytest-ordering 插件概述 Pytest-ordering 是一个用于控制测试用例执行顺序的插件。该插件允许开发者通过简单的装饰器定义不同测试之间的相对顺序。 ### 安装方法 为了安装 pytest-ordering 插件,在终端中可以使用 `pip` 工具并运行如下命令: ```bash pip install pytest-ordering ``` 对于另一个类似的库 pytest-order,也可以采用相似的方式进行安装[^2]。 ### 使用教程 #### 控制测试用例执行顺序的方法 pytest-ordering 提供了几种不同的方式来设置测试用例的执行顺序。最常见的是利用带有参数的 `@pytest.mark.run()` 装饰器,其中 `order` 参数决定了具体的排列位置;数值越低表示优先级越高,意味着更早被执行。例如: ```python import pytest @pytest.mark.run(order=1) def test_first(): assert True, "This should run first" @pytest.mark.run(order=2) def test_second(): assert True, "This will follow the previous one" ``` 除了显式的数字排序外,还存在一些特殊的标记用来简化某些特定情况下的操作,比如让某个测试总是位于队列开头或结尾。不过需要注意的是,部分早期版本中存在的标签如 `first` 和 `last` 在较新的发行版里已被移除不再推荐使用[^4]。 #### 示例代码展示如何应用这些特性 下面给出了一组完整的例子说明怎样运用上述提到的功能点: ```python import pytest # 设置此测试最先执行 @pytest.mark.run(order=-2) def test_negative_order_example(): print("\nTest with negative order value.") pass # 正常按序号指定先后关系 @pytest.mark.run(order=1) def test_positive_order_one(): print("\nFirst ordered positive number.") pass @pytest.mark.run(order=3) def test_larger_than_default(): print("\nLargest defined position.") pass # 默认情况下未指明次序则放在最后处理 def test_without_explicit_order(): print("\nNo explicit ordering applied here.") pass # 小于零会被安排到非常靠前的位置 @pytest.mark.run(order=-1) def test_most_prioritized(): print("\nMost prioritized due to smallest integer used as its parameter.") pass ``` 当这段脚本被 pytest 执行时,输出会按照设定好的序列依次打印出来,从而验证了各个测试案例确实依照预期进行了编排。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值