PAT日志 1146「建议收藏」

PAT日志 1146「建议收藏」顽强的小白1146TopologicalOrder(25分)ThisisaproblemgivenintheGraduateEntranceExamin2018:WhichofthefollowingisNOTatopologicalorderobtainedfromthegivendirectedgraph?Nowyouare…

大家好,又见面了,我是你们的朋友全栈君。

顽强的小白

1146 Topological Order (25 分)

This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.
在这里插入图片描述

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 1,000), the number of vertices in the graph, and M (≤ 10,000), the number of directed edges. Then M lines follow, each gives the start and the end vertices of an edge. The vertices are numbered from 1 to N. After the graph, there is another positive integer K (≤ 100). Then K lines of query follow, each gives a permutation of all the vertices. All the numbers in a line are separated by a space.

Output Specification:

Print in a line all the indices of queries which correspond to “NOT a topological order”. The indices start from zero. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line. It is graranteed that there is at least one answer.

Sample Input:

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:

3 4

题目解析

判断所给的序列是不是拓扑排序
我的思路很暴力,拓扑排序本来就是一个一个拿掉图上的顶点,这些被拿掉的顶点只有一个要求,就是不能有其他顶点指向它,因此我的思路就是,判断当点要拿掉的结点有没有被谁指着,如果有就不是拓扑排序。

代码实现

#include <cstdio> 
#include <vector> 
#include <algorithm> 

using namespace std; 

const int maxn=1005;

bool vis[maxn];
int G[maxn][maxn];
vector<int> ans;int main(){ 
   
 fill(G[0],G[0]+maxn*maxn,0);
 int n,e,u,v;
 scanf("%d%d",&n,&e);
 for(int i=0;i<e;++i){ 
   
  scanf("%d%d",&u,&v);
  G[u][v]=1;
 }
 int m;
 scanf("%d",&m);
 for(int i=0;i<m;++i){ 
   
  fill(vis,vis+n+1,true); //初始状态 灯全亮 
  int flag=0;
  for(int j=0;j<n;++j){ 
   
   scanf("%d",&u);
   for(int k=1;k<=n;++k){ 
   
    if(vis[k]==true&&G[k][u]==1){ 
   
     flag=1;
     break; 
    }
   }
   vis[u]=false;   //把这个顶点灭灯 
  }
  if(flag==1) ans.push_back(i);
 }
 for(int i=0;i<ans.size();++i) { 
   
  printf("%d",ans[i]);
  if(i<ans.size()-1) printf(" ");
  else printf("\n");
 }
} 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133288.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • readandwritelock_读写锁使用场景

    readandwritelock_读写锁使用场景ReadWriteLock管理一组锁,一个是只读的锁,一个是写锁。读锁可以在没有写锁的时候被多个线程同时持有,写锁是独占的。所有读写锁的实现必须确保写操作对读操作的内存影响。换句话说,一个获得了读锁的线程必须能看到前一个释放的写锁所更新的内容。读写锁比互斥锁允许对于共享数据更大程度的并发。每次只能有一个写线程,但是同时可以有多个线程并发地读数据。ReadWriteLock适用于读多写少的并发

    2022年8月12日
  • 阶乘算法简单实现

    阶乘算法简单实现longintfac1(intn){ staticlongintf=1; f*=n; returnf;}longintfac2(intn){ longintf=1; if(n==0|n==1) {  returnf; } else {  f=n*fac2(n-1); }}voidmain(){ inti; for(i=1;i {  printf(“fac1[%d!]=%ld/n”,i,fac1(i)); } printf(“fac2[8!]=%ld/n”,fac2(8));

    2022年7月24日
  • 密码–暴力激活成功教程

    密码–暴力激活成功教程1.海德拉(Hydra)hydra是世界顶级密码暴力密码激活成功教程工具,支持几乎所有协议的在线密码激活成功教程,功能强大,其密码能否被激活成功教程关键取决于激活成功教程字典是否足够强大,在网络安全渗透过程中是一款必备的测试工具。root@kali2019:~#hydra-Luserlist.txt-Ppasswordlist.txt-t20192.168.106.134ssh我们先建立简单的字典…

    2022年8月22日
  • repos_there are no enabled repos

    repos_there are no enabled repos配置Base源,使用163或者阿里的1、mirrors.163.com/2、mirrors.aliyun.com/or https://developer.aliyun.com/mir

    2022年8月4日
  • 遗传算法优化bp神经网络matlab代码_神经网络进化算法

    遗传算法优化bp神经网络matlab代码_神经网络进化算法最近在学遗传算法优化BP神经网络,从新浪博客,Matlab中文论坛以及《MATLAB神经网络43个案例分析》里看了许多资料,存在着缺少test函数,以及函数名调用错误等问题。自编了test函数,调整后,供大家参考,(在Matlab2006a亲测可行)。参考文献:《MATLAB神经网络的43个案例分析》王小川,史峰,郁磊等,北京航空航天大学出版社。flyingnosky的sina博…

    2022年9月11日
  • 进程调度与进程切换_模式切换和进程切换有什么区别

    进程调度与进程切换_模式切换和进程切换有什么区别从今天开始,我们将要开启一个新的系列【闪耀计划】,没错!这是今年上半年的一整个系列计划!本专题目的是通过百天刷题计划,通过题目和知识点串联的方式,完成对计算机操作系统的复习和巩固;同时还配有专门的笔记总结和文档教程哦!想要搞定,搞透计算机操作系统的同学,本专栏将会通过模块化的分类,刷够1000道题,为大家提供点对点的考点相关知识轰炸!值得注意的是,本专栏将会通过教程+课后习题的方式来进行巩固教学,课后习题的题量也是算入总题数的哦!

    2022年10月20日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号