拓扑排序入门两种模板

一:深度优先遍历(dfs)

运用递归从后往前找,先递归到尽头找出度为零的点,不断插入到数组首即可。

使用了刘汝佳老师的代码,顺便还判断了是否有有向环。

#include <bits/stdc++.h>

using namespace std;
#define maxn 105
int matrix[maxn][maxn], vis[maxn], topo[maxn], n, m, t;
//-1 正在访问,判断是否有环。0 未访问 1 已访问
bool dfs(int u) {
    vis[u] = -1;
    for(int i = 1; i <= n; i++) {
        if(matrix[u][i]) {
            if(vis[i]<0) return false;
            else if(!vis[i]&&!dfs(i)) return false;
        }
    }
    vis[u] = 1;
    topo[--t] = u;
    return true;
}

bool toposort() {
    t = n;
    for(int i = 1; i <= n; i++) {
        if(!vis[i])
            if(!dfs(i)) return false;
    }
    return true;
}

int main() {
    freopen("i.txt","r",stdin);
    while (cin >> n >> m && (n || m)) {
        memset(matrix, 0, sizeof(matrix));
        memset(topo, 0, sizeof(topo));
        int x, y;
        for (int i = 0; i < m; i++) {
            cin >> x >> y;
            matrix[x][y] = 1;
        }
        if(toposort()) {
            for(int i = 0; i < n; i++)
                cout << topo[i] << " ";
        }
    }
}

二:Kahn模板

需要在建图的同时统计各个点的入度个数。然后每次取出入度为0的顶点删掉,并删掉和该点有关的边,需要维护一个入度为0的队列或者栈。

#include <bits/stdc++.h>

using namespace std;
#define maxn 105
int matrix[maxn][maxn], indegree[maxn], topo[maxn], n, m;

void toposort() {
    int cnt = 0;
    queue<int> queue1;
    for(int i = 1; i <= n; i++) {
        if(!indegree[i])
            queue1.push(i);
    }
    while(!queue1.empty()) {
        int temp = queue1.front(); queue1.pop();
        topo[cnt++] = temp;
        for(int i = 1; i <= n; i++) {
            if(matrix[temp][i]) {
                indegree[i]--;
                if(!indegree[i])
                    queue1.push(i);
            }
        }
    }
}

int main() {
    freopen("i.txt","r",stdin);
    while (cin >> n >> m && (n || m)) {
        memset(matrix, 0, sizeof(matrix));
        memset(indegree, 0, sizeof(indegree));
        int x, y;
        for (int i = 0; i < m; i++) {
            cin >> x >> y;
            matrix[x][y] = 1;
            indegree[y]++;
        }
        toposort();
        for(int i = 0; i < n; i++)
            cout << topo[i] << " ";
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值