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


相关推荐

  • sql中的convert转换数字_Convert

    sql中的convert转换数字_Convert   一般写程序是用的都是Convert.ToInt32,为什么呢?1.Convert.ToInt是数据类型转换成int类型2.   有三种方法toint16,toint32,toint64   int16-数值范围:-32768到32767   int32-数值范围:-2,147,483,648到2,147,483,647   int64…

    2022年8月15日
    4
  • PID控制算法仿真_连续控制系统的充分必要条件

    PID控制算法仿真_连续控制系统的充分必要条件引言PID是Proportional(比例)、Integral(积分)、Differential(微分)三者的缩写。PID调节是连续控制系统中技术最成熟、应用最广泛的调节方式。PID调节实质是根据输入的偏差值,按照比例、积分、微分的函数关系进行运算,运算结果用以控制输出。之前在项目中也用到过不少PID的算法,但大多属于一知半解的状态,或者胡乱调节的程度,最近在学习的过程偶然对PI…

    2022年4月19日
    89
  • 使用BIOS进行键盘输入【编程:字符串的输入】

    使用BIOS进行键盘输入【编程:字符串的输入】

    2021年8月21日
    65
  • PHP入门:在Windows中安装PHP工作环境

    PHP入门:在Windows系统中分别安装PHP工作环境一、什么是LAMP?Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各

    2021年12月20日
    48
  • 微信公众号开发者社区_订阅号怎么弄

    微信公众号开发者社区_订阅号怎么弄引言及内容概要已经有几位读者抱怨“柳峰只用到文本消息作为示例,从来不提图文消息,都不知道图文消息该如何使用”,好吧,我错了,原本以为把基础API封装完、框架搭建好,再给出一个文本消息的使用示例,大家就能够照猫画虎的,或许是因为我的绘画功底太差,画出的那只猫本来就不像猫吧……本篇主要介绍微信公众帐号开发中图文消息的使用,以及图文消息的几种表现形式。标题取名为“图文消息全攻略”,这绝对不是标题

    2022年9月29日
    2
  • pyd文件介绍

    pyd文件介绍pyd 一般是 python 外的其他语言如 C C 编写的 python 扩展模块 即 python 的一个动态链接库 与 dll 文件相当 在 linux 系统中一般为 so 文件 也有的时候 为了对 python 文件进行加密 会把 python 模块编译成 pyd 文件 供其他人使用 拿到一个 pyd 文件 在没有文档说明的情况下 可以试试查看模块内的一些函数和类的用法 首先 importXXX pyd 的文件名 然后直接 print dir XXX print help XXX 其中 dir 列出了属性和方法 help

    2025年8月12日
    2

发表回复

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

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