poj2367(拓扑排序)

题目就是让你求拓扑序
拓扑排序就是,先找到入度为0的点,删去,同时把它的所有出度删去,再找新的入度为0的点,删去的点的顺序就是拓扑序

#include <cstdio> 
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int s[233][233];
int in_degree[2333];
int a[2333],n;
int out[2333];
bool vis[2333];
int main()
{
    scanf("%d",&n);
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; ; j ++)
        {
            scanf("%d",&a[j]);
            if(a[j] == 0)
            break;
            s[i][a[j]] = 1;
            in_degree[a[j]]++;
        }
    }
    for(int i = 1; i <= n; i ++)
    {
        int t = 0;
        for(int j = 1; j <= n; j ++)
        {
            if(in_degree[j] == 0 && !vis[j])
            {
                vis[j] = 1;
                t = j;
                break;
            }
        }
        if(t == 0)
        break;
        else
        out[i] = t;
        for(int j = 1; j <= n; j ++)
        {
            if(s[t][j] == 1)
            in_degree[j]--;
        }
    }
    for(int i = 1; i <= n; i ++)
    cout <<out[i]<<" ";
    return 0;
}
    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/cherish_k/article/details/52945945
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞