POJ2367【拓扑排序】

很裸的拓扑排序~

//#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
using namespace std;

typedef __int64 LL;

const int N=1e2+10;
int ma[N][N];
int pre[N];
int n;

void tuopu()
{
    int k;
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        k=0;
        for(int j=1;j<=n;j++)
        {
            if(!pre[j])
            {
                pre[j]=-1;
                if(flag) printf(" ");
                printf("%d",j);
                flag=1;
                k=j;
                break;
            }
        }
        if(!k)
            break;
        for(int j=1;j<=n;j++)
        {
            if(pre[j]!=-1&&ma[k][j])
            {
                pre[j]--;
            }
        }
    }
}

int main()
{
    scanf("%d",&n);
    memset(ma,0,sizeof(ma));
    memset(pre,0,sizeof(pre));
    for(int i=1;i<=n;i++)
    {
        while(1)
        {
            int x;
            scanf("%d",&x);
            if(!x)
                break;
            ma[i][x]=1;
            pre[x]++;
        }
    }
    tuopu();
    return 0;
}
原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934859.html