ZJU PTA ds 6-1 Is Topological Order

6-1 Is Topological Order

Write a program to test if a give sequence Seq is a topological order of a given graph Graph.

Format of functions:

bool IsTopSeq( LGraph Graph, Vertex Seq[] );

where LGraph is defined as the following:

typedef struct AdjVNode *PtrToAdjVNode; 
struct AdjVNode{
    Vertex AdjV;
    PtrToAdjVNode Next;
};

typedef struct Vnode{
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode{  
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGNode LGraph;

The function IsTopSeq must return true if Seq does correspond to a topological order; otherwise return false.

Note: Although the vertices are numbered from 1 to MaxVertexNum, they are indexed from 0 in the LGraph structure.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>

typedef enum {false, true} bool;
#define MaxVertexNum 10  /* maximum number of vertices */
typedef int Vertex;      /* vertices are numbered from 1 to MaxVertexNum */

typedef struct AdjVNode *PtrToAdjVNode; 
struct AdjVNode{
    Vertex AdjV;
    PtrToAdjVNode Next;
};

typedef struct Vnode{
    PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];

typedef struct GNode *PtrToGNode;
struct GNode{  
    int Nv;
    int Ne;
    AdjList G;
};
typedef PtrToGNode LGraph;

LGraph ReadG(); /* details omitted */

bool IsTopSeq( LGraph Graph, Vertex Seq[] );

int main()
{
    int i, j, N;
    Vertex Seq[MaxVertexNum];
    LGraph G = ReadG();
    scanf("%d", &N);
    for (i=0; i<N; i++) {
        for (j=0; j<G->Nv; j++)
            scanf("%d", &Seq[j]);
        if ( IsTopSeq(G, Seq)==true ) printf("yes\n");
        else printf("no\n");
    }

    return 0;
}

/* Your function will be put here */

Sample Input (for the graph shown in the figure):

在这里插入图片描述

6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6

Sample Output:

yes
yes
yes
no
no

My code

#include <string.h>
/* Your function will be put here */
bool IsTopSeq( LGraph Graph, Vertex Seq[] )
{
    // 遍历Seq[],如果当前元素在入度为0,则说明可以为一个拓扑排序
    // 否则,不能为一个拓扑排序,因此,不需要打印,关心的问题是这个元素
    // 入度是否为0,可以类似于一个哈希表
    
    // 入度
    int inDegree[MaxVertexNum];
    // 初始化
    Vertex v;
    // memset(inDegree, 0, sizeof(inDegree));
    /* 初始化Indegree[] */
    for (v=0; v<Graph->Nv; v++)
        inDegree[v] = 0;
    // 得到入度
    PtrToAdjVNode w;
    for (v = 0; v < Graph->Nv; v++) {// 头节点(头指针)数组
        for (w = Graph->G[v].FirstEdge; w != NULL; w = w->Next) {
            // 如果W不指向空,则说明W指向的元素标号可以入度+1
            inDegree[w->AdjV]++;
//             printf("%d\n", w->AdjV);
//             printf("inDegree[%d] = %d\n", w->AdjV,inDegree[w->AdjV]);
        }
    }
    int flag = 1;
    
    //printf("sizeof(Seq) = %d  sizeof(Seq[0]) = %d %d\n", sizeof(Seq), sizeof(Seq[0]),(int)(sizeof(Seq) / sizeof(Seq[0])));
    

    for (int i = 0; i < Graph->Nv; i++) {
        // if (inDegree[Seq[i]] != 0) {// 入度不为0
        if (inDegree[Seq[i] - 1] != 0) {// 入度不为0
            flag = 0;
            break;
        }
        // 入度为0时,剔除,即将其入度改成不为0即可
        inDegree[Seq[i] - 1]--;
        // 将Seq[i]的每一个邻接点的入度-1
        for (w = Graph->G[Seq[i] - 1].FirstEdge; w != NULL; w = w->Next) {
            inDegree[w->AdjV]--;
        }
    }

    if (flag == 0) {
        return false;
    }
    else {
        return true;
    }
}

题目中的一句话很容易被忽视:
Note: Although the vertices are numbered from 1 to MaxVertexNum, they are indexed from 0 in the LGraph structure.

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值