JOJ 1089 & ZOJ 1060 & poj 1094 Sorting It All Out (邻接表的栈拓扑排序模板)

Description

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

Output

For each problem instance, output consists of one line. This line should be one of the following three:

Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.

where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.

Sample Input

4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

Sample Output

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.

这道题WA了2天,基本都没做其他的题,BUG出了很多,主要可能是有一个数据是同时能够可拓扑且有环,如果判断的顺序不对的话,会挂掉的,顺序换一下就轻松水过了。
简单的拓扑排序题,主要是练一下邻接表的拓扑排序,先贴模板
//邻接表的toposort
#include <cstdio>
#include <string.h>const int maxn = 50;
struct Node
{
    int to;//保存终点
    struct Node * next ;
};
int n,m;
Node * List[maxn];
int indeg[maxn];
int ans[100];
void toposort ()
{
    int i,top=-1,t=0;
    Node *tmp;
    bool flag=false;//初始化为无环
    for (i=0 ; i<n ; i++)
      if(!indeg[i])//如果i点入度为0,则将其压入栈中
        indeg[i]=top,top=i;
        //入栈操作 ,top指向栈顶,用indeg[]来存栈中其他元素和空指针-1,因为原来的入度数据已经不需要了
    for (i=0 ; i<n ; i++)
    {
        if(top==-1)// 如果top指向-1 则数据中存在环
        {flag=true ; break ;}
        //否则 继续将元素加入答案列表里
        else
        {
            int j=top;top=indeg[top];
            //出栈操作
            ans[t++]=j+1;
            tmp=List[j];
            while (tmp!=NULL)
            {
                int k=tmp->to;
                if(--indeg[k]==0)
                indeg[k]=top,top=k;
                tmp=tmp->next;
            }
        }
    }
    if(flag)printf("Network has a cycle!\n");
    else
    {
        for (i=0 ; i<t ; i++)
        printf("%d%c",ans[i],i!=t-1?' ':'\n');
    }
}int main()
{
    int i,u,v;
    //freopen ("in.txt","r",stdin);
    while (scanf("%d%d",&n,&m), n && m)
    {
        memset (List , 0 , sizeof(List));
        memset (indeg , 0 , sizeof(indeg));
        Node *tmp;
        for (i=0 ; i<m ; i++)
        {
            scanf("%d%d" , &u, &v);
            u-- ; v-- ;//1 is mapping 0.
            indeg[v]++;//增加终点的indegree.
            tmp=new Node ;
            tmp->to=v;
            tmp->next = NULL;//这步是必要的
            if(List[u]==NULL)//List 为空时,直接赋值,否则插入
             List[u]=tmp;
            else
             tmp->next=List[u],List[u]=tmp;
        }        toposort();        for (i=0 ; i<n ; i++)
        {
            tmp=List [i];
            while (tmp!=NULL)
            {
                List[i]=tmp->next; delete tmp ; tmp=List[i];
            }
        }
    }
    return 0;
}


之后是AC代码,WA了10+次才AC的T T。。
#include <cstdio>
#include <string.h>const int maxn = 30;
struct Node
{
    int to;//保存终点
    struct Node * next ;
};int n,m,t,step,suc,Stack_num;
Node * List[maxn];
int indeg[maxn],indeg2[maxn],tt[maxn],ans[100];
bool find_topo,ffind,same,incon;void toposort ()
{
    int i,top=-1;
    t=0;
    for(i=0 ; i<n ; i++)
     indeg2[i]=indeg[i];
    Node *tmp;
    for (i=0 ; i<n ; i++)
      if(!indeg2[i])//如果i点入度为0,则将其压入栈中
        tt[i]=top,top=i,Stack_num++;
    if(Stack_num>1)find_topo=false;
    for (i=0 ; i<n ; i++)
    {
        if(top==-1)// 如果top指向-1 则数据中存在环
        {incon=true ; break; }
        else
        {
            int j=top;top=tt[top];
            Stack_num--;
            ans[t++]=j;//printf("%d\t",t);
            tmp=List[j];
            while (tmp!=NULL)
            {
                int k=tmp->to;
                if(--indeg2[k]==0)
                tt[k]=top,top=k,Stack_num++;
                tmp=tmp->next;
            }if(Stack_num>1 )find_topo=false;
        }
    }
}
int main()
{
    int i,u,v;
    char input[10];
    freopen ("input.txt","r",stdin);
    //freopen ("out.txt","w",stdout);
    while (scanf("%d%d",&n,&m), n && m)
    {
        memset (List , 0 , sizeof(List));
        memset (indeg , 0 , sizeof(indeg));
        memset (tt,0,sizeof(tt));
        Node *tmp;
        ffind=incon=false;//        for (i=0 ; i<m ; i++)
        {
            scanf("%s" , input);
            if(!(incon||ffind))
            {
                find_topo=true;
                u=input[0]-'A';v=input[2]-'A';
                Node *tmp=new Node ;
                tmp->next=NULL;
                tmp->to=v;
                indeg[v]++;
                if(List[u]==NULL)
                 List[u]=tmp;
                else
                 tmp->next=List[u],List[u]=tmp;
                toposort();
                step=i;//step+1
                if(find_topo)ffind=true;
            }
        }
        if(incon)   
         printf("Inconsistency found after %d relations.\n",step+1);
        else if(ffind)
        {
            printf("Sorted sequence determined after %d relations: ",step+1);
            for(i=0;i<n;i++)
                printf("%c",'A'+ans[i]);
            printf(".\n");
        }
        else    printf("Sorted sequence cannot be determined.\n");
    }
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值