拓扑排序

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct arcnode
{
    int to;
    struct arcnode *next;
};

int n,m;
arcnode *l[100];
int cnt[100];
char output[100];

void topsort()
{
    int i,top=-1;
    arcnode *temp;
    bool becycle=0;
    int pos=0;
    for(i=0; i<n; i++)
    {
        if(cnt[i]==0)
        {
            cnt[i]=top;
            top=i;
        }
    }
    for(i=0; i<n; i++)
    {
        if(top==-1)
        {
            becycle=1;
            break;
        }
        else
        {
            int j=top;
            top=cnt[top];
            pos+=sprintf(output+pos,"%d ",j+1);
            temp=l[j];
            while(temp!=NULL)
            {
                int k=temp->to;
                if(--cnt[k]==0)
                {
                    cnt[k]=top;
                    top=k;
                }
                temp=temp->next;
            }
        }
    }
    if(becycle) printf("network has a cycle!
");
    else
    {
        int len=strlen(output);
        output[len-1]=0;
        printf("%s
",output);
    }
}

int main()
{
    int i,u,v;
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        memset(l,0,sizeof(l));
        memset(cnt,0,sizeof(cnt));
        memset(output,0,sizeof(output));
        arcnode *temp;
        for(i=0; i<m; i++)
        {
            scanf("%d%d",&u,&v);
            u--;
            v--;
            cnt[v]++;
            temp=new arcnode;
            temp->to=v;
            temp->next=NULL;
            if(l[u]==NULL)
                l[u]=temp;
            else
            {
                temp->next=l[u];
                l[u]=temp;
            }
        }
        topsort();
        for(i=0; i<n; i++)
        {
            temp=l[i];
            while(temp!=NULL)
            {
                l[i]=temp->next;
                delete temp;
                temp=l[i];
            }
        }
    }
    return 0;
}

/*
6 8
1 2
1 4
2 6
3 2
3 6
5 1
5 2
5 6
6 8
1 3
1 2
2 5
3 4
4 2
4 6
5 4
5 6
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

原文地址:https://www.cnblogs.com/xryz/p/4847907.html