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


相关推荐

  • Idea激活码永久有效Idea2018.1.7激活码教程-持续更新,一步到位

    Idea激活码永久有效Idea2018.1.7激活码教程-持续更新,一步到位Idea激活码永久有效2018.1.7激活码教程-Windows版永久激活-持续更新,Idea激活码2018.1.7成功激活

    2022年6月17日
  • java io常用类总结

    java io常用类总结读写数据是java开发过程中经常需要做的,本篇文章将我们平时常用的javaio处理类做个总结,同时给出相关demo。可以看得出来,JAVAIO主要有这四个类InputStream、OutputStream、Reader、Writer来处理,要处理字节流的就用InputStream、OutputStream,要处理字符流,就用Reader、Writer,现实中根据需要,我们选择他们的相关子类进行…

    2022年5月9日
  • sqlbulkcopy 使用DataTable作为数据源的数据类型问题–来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier…

    sqlbulkcopy 使用DataTable作为数据源的数据类型问题–来自数据源的String类型的给定值不能转换为指定目标列的类型 uniqueidentifier…今天做批量插入的时候,SQLSERVER总是报错,错误提示“来自数据源的String类型的给定值不能转换为指定目标列的类型uniqueidentifier”。首先核对了一下定义的dataTable中的DataColumn[]的顺序和数量,发现和数据库的表结构是一致的,而且把代码中对dataRow[]对应位置赋值的语句屏蔽后,执行成功。因此可以确定主要还是由于类型转换的时候的问题。后来…

    2022年7月20日
  • Bootstrap使用及环境搭建详解

    Bootstrap使用及环境搭建详解Bootstrap官网官网:https://getbootstrap.com/中文网:http://www.bootcss.com/为什么要使用Bootstrap?首先,观察Bootstrap文件树(下图)不难发现,文件都是我们常见的一些css、js文件。Bootstrap为我们写好测试了各种兼容、疑难问题,并构建了一套非常优秀成熟的响应式类,并及时提供了移动端优先的响应式系统,我们只需…

    2022年10月27日
  • oracle修改用户的密码_修改linux用户密码

    oracle修改用户的密码_修改linux用户密码oracle中修改用户密码首先是win键+R输入cmd输入sqlplus/assysdba,回车,此时进入到SQL>输入alterusersystemidentifiedbysystem;这行代码的意思就是修改system用户的密码为system(注意:代码末尾要加上;分号)…

    2022年7月28日
  • npm ERR! code E404 npm ERR! 404 Not Found – GET https://registry.npmjs.com/@mlamp%2fuser-info-dropdo

    npm ERR! code E404 npm ERR! 404 Not Found – GET https://registry.npmjs.com/@mlamp%2fuser-info-dropdonpmERR!codeE404npmERR!404NotFound-GEThttps://registry.npmjs.com/@mlamp%2fuser-info-dropdown-Notfound当我npminstall的时候出现这个错误原因是npm源指向的问题执行:npmconfigsetregistryhttps://registry.npmjs.org/问题的原因出现在:在Vue/react/angular框架中打包和编译时报错。使用指令为项目

    2022年10月25日

发表回复

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

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