• 问题 D: 是否为有效的拓扑序列


    题目描述

    在一个有向无环图中,可能存在多种有效拓扑序列。以下图为例:

    存在两种可行的拓扑序列
    0 1 2 3 4
    0 2 1 3 4
    本题会给出一个图,以及多个序列,你来判断每一个序列是否是该图的有效拓扑序列。 

    输入格式

    第一行为2个正整数m和n,分别表示有向无环图的节点个数和边的数量。
    接下来n行,代表n条边。分别是起点、终点、权重,以空格分隔。
    (m<50,n<100)
    接下来为一个正整数o,表示接下来会出现的序列个数。
    (o<1000)
    再往后是o个序列,序列中的每个值用空格隔开。

    输出格式

    按行输出:o个序列中,每一个序列是否为图的有效拓扑序列。
    是的话输出:YES
    不是的话输出:NO

    输入样例

    1. 5 6
    2. 0 1
    3. 0 2
    4. 1 3
    5. 2 3
    6. 2 4
    7. 3 4
    8. 3
    9. 0 1 2 3 4
    10. 0 2 1 3 4
    11. 0 1 3 2 4

    输出样例  

    1. YES
    2. YES
    3. NO

    代码展示 

    思路:循环判断当前结点入度,为零则将其邻接点入度-1,继续判断给定序列的下一结点。若为有效拓扑序列,(入度)则应按序陆续减为0;否则无效。

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. #define INFO_MAX_SIZE 20
    7. #define MAX_SIZE 200
    8. //领接矩阵存储的图
    9. struct Graph{
    10. int vexNumber;
    11. string vexInfo[INFO_MAX_SIZE];
    12. int adjMatrix[MAX_SIZE][MAX_SIZE];
    13. };
    14. //弧结点定义
    15. struct ArcNode{
    16. int weight;//弧上的信息部分
    17. int adj;//邻接点的序号
    18. ArcNode *nextarc;
    19. };
    20. //顶点结点定义
    21. struct VexNode{
    22. string Info;
    23. ArcNode *firstarc;
    24. };
    25. //领接表结构的图的定义
    26. struct linkGraph{
    27. VexNode *vexes;
    28. int vexnumber;
    29. };
    30. struct tempNode{
    31. int col;
    32. int row;
    33. //tempNode *next;
    34. };
    35. struct temp{
    36. int num;
    37. tempNode *docu;
    38. };
    39. int preInitGraph(linkGraph &G,const Graph &g){
    40. G.vexes=new VexNode[g.vexNumber];
    41. G.vexnumber=g.vexNumber;
    42. for(int i=0;i
    43. G.vexes[i].firstarc=NULL;
    44. }
    45. return 0;
    46. }
    47. //将邻接矩阵存储的图转换为领接表存储的图
    48. void InitGraph(linkGraph &G,const Graph &g,temp &t){
    49. preInitGraph(G,g);
    50. for(int i=0;i
    51. int a,b;
    52. a=t.docu[i].row;b=t.docu[i].col;
    53. ArcNode *p=new ArcNode();
    54. p->nextarc=NULL;
    55. p->adj=b;
    56. ArcNode *q=G.vexes[a].firstarc;
    57. if(G.vexes[a].firstarc==NULL)
    58. G.vexes[a].firstarc=p;
    59. else{
    60. while(q->nextarc!=NULL){
    61. q=q->nextarc;
    62. }
    63. q->nextarc=p;
    64. }
    65. }
    66. }
    67. //输出领接表存储的图
    68. void PrintGraph(const linkGraph &G){
    69. for(int i=0;i
    70. cout<
    71. ArcNode *p=G.vexes[i].firstarc;
    72. cout<
    73. while(p!=NULL){
    74. cout<<" --> "<adj;
    75. p=p->nextarc;
    76. }
    77. cout<
    78. }
    79. }
    80. int check(linkGraph LG,int a[],int indegree[]){
    81. //PrintGraph(LG);
    82. int temp[LG.vexnumber]={0};
    83. for(int i=0;i
    84. for(int i=0;i
    85. if(temp[a[i]]!=0) return 1;
    86. for(ArcNode *p=LG.vexes[a[i]].firstarc;p!=nullptr;p=p->nextarc){
    87. temp[p->adj]--;
    88. }
    89. }
    90. return 0;
    91. }
    92. int main(){
    93. //freopen("/config/workspace/test/test","r",stdin);
    94. int n,m;
    95. cin>>n>>m;
    96. Graph G;
    97. G.vexNumber=n;
    98. temp t;
    99. t.num=m;
    100. t.docu=new tempNode[m];
    101. for(int i=0;i
    102. int a,b;
    103. cin>>a>>b;
    104. t.docu[i].row=a;
    105. t.docu[i].col=b;
    106. }
    107. linkGraph LG;
    108. InitGraph(LG,G,t);
    109. int indegree[LG.vexnumber]={0};
    110. //for(int i=0;i
    111. for(int i=0;i
    112. for(ArcNode *p=LG.vexes[i].firstarc;p!=nullptr;p=p->nextarc)
    113. indegree[p->adj]++;
    114. }//入度
    115. int k;
    116. cin>>k;
    117. for(int i=0;i
    118. int a[n];
    119. for(int j=0;j
    120. cin>>a[j];
    121. }
    122. int flag=check(LG,a,indegree);
    123. if(flag) cout<<"NO"<
    124. else cout<<"YES"<
    125. }
    126. return 0;
    127. }

    //闲叙题外话:这周事情好多啊。

  • 相关阅读:
    SQL优化面试专题
    Java的一些常见类【万字介绍】
    logback--基础--02--配置--configuration
    关于融合软件运行unity程序被闪退解决方案
    实现一个极简的字节数组对象池
    编译器安全
    dask读取sql数据:MySQL
    【WebLogic】OPatch Patches: No patches installed
    SSRF漏洞复现(redis)
    机车整备场数字孪生 | 图扑智慧铁路
  • 原文地址:https://blog.csdn.net/weixin_65908362/article/details/127989923