UVa 10305

来源:互联网 发布:思科网络技术学院官网 编辑:程序博客网 时间:2024/05/16 07:24

拓扑排序,开始想图省事结果超时了。。。还是正常写吧。。。。


#include <stdio.h>#include <string.h>int G[101][101];int select[101];int indegree[101];int stack[101];int nodes;int mark=0;int ini(){    for(int i=0;i<101;++i)    {        select[i]=0;        for(int j=0;j<101;++j)        {            G[i][j]=0;        }        indegree[i]=0;        stack[i]=0;    }}int is_done(){    for(int i=0;i<nodes;++i)    {        if(select[i]==0)        return 0;    }    return 1;}/*void topology(){    while(1)    {        for(int i=0;i<nodes;++i)        {            if(indegree[i]==0)            {                if(mark)                    printf(" ");                mark=1;                printf("%d",i+1);                select[i]=1;                for(int j=0;j<nodes;++j)                {                    if(G[i][j]&&!select[j])                    {                        indegree[j]--;                    }                }            }        }        if(is_done())break;    }}*/void topology(){    int top=-1;    int tx;    for(int i=0;i<nodes;++i)    {        if(indegree[i]==0)        {           stack[++top]=i;        }    }    while(top!=-1)    {        tx=stack[top--];        printf("%d ",tx+1);        for(int j=0;j<nodes;++j)        {            if(G[tx][j])            {                indegree[j]--;                if(indegree[j]==0)                {                    stack[++top]=j;                }            }        }    }}int main(){    int c;    while(scanf("%d%d",&nodes,&c)!=EOF)    {        ini();        if(c==0&&nodes==0)return 0;        for(int i=0;i<c;++i)        {            int a,b;            scanf("%d%d",&a,&b);            G[a-1][b-1]=1;            indegree[b-1]++;        }        /*        printf("sadas\n");        for(int k=0;k<nodes;++k)        {            printf("%d ",indegree[k]);        }        printf("\n");*/        topology();        printf("\n");    }    return 0;}


原创粉丝点击