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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • zookeeper的基本操作_奇门遁甲实战入门之五

    zookeeper的基本操作_奇门遁甲实战入门之五本原创入门教程,涵盖ZooKeeper核心内容,通过实例和大量图表,结合实战,帮助学习者理解和运用,任何问题欢迎留言。目录:zookeeper介绍与核心概念 安装和使用 ZooKeeper分布式锁实现 ZooKeeper框架Curator分布式锁实现及源代码分析 zookeeper开发实战(java客户端)本章是后续学习的基石,只有充分理解了分布式系统的概念和面临的问题,以及Z…

    2022年10月7日
    0
  • CLion 激活码 2021 8月(JetBrains全家桶)「建议收藏」

    (CLion 激活码 2021 8月)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~1STL5S9V8F-eyJsaWNlbnNlSWQiOi…

    2022年3月27日
    96
  • navicat永久激活码最新【2021免费激活】

    (navicat永久激活码最新)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~0X1Z…

    2022年3月28日
    113
  • db2 timestampdiff

    db2 timestampdiff要将字符串转换成日期或时间值,可以使用:TIMESTAMP(‘2002-10-20-12.00.00.000000’)TIMESTAMP(‘2002-10-2012:00:00’)DATE(‘2002-10-20′)DATE(’10/20/2002′)TIME(’12:00:00’)TIME(‘12.00.00’)TIMESTAMP()、DATE(…

    2022年6月12日
    37
  • latex 公式换行的命令

    latex 公式换行的命令2019独角兽企业重金招聘Python工程师标准>>>…

    2022年6月10日
    37
  • webgame开发中配置存储的介绍「建议收藏」

    webgame开发中配置存储的介绍「建议收藏」webgame世界的基础数值都是事先配置好的,在运行的时候可以随时读取,是属于非常重要和必不可少的部分,而且数据量也不少。这样的数据在开发中的存储也就变得重要了,需要保证效率、性能、安全等,一般的做法有使用xml文本文件保存、静态数组保存和数据库直接保存。 xml文本文件保存优点: 共享性强,几乎每种语言都可以读取和解析xml文件,方便多语言混合开发。 结构清晰,xml的结构很

    2022年5月2日
    41

发表回复

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

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