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

题意:

做这题之前首先要先去了解什么是拓扑排序,可以参考https://blog.csdn.net/qq_35644234/article/details/60578189

给出一个图,再给几组数据,让你判断这几组数据是否符合拓扑排序

题解:

保存入度数和出度的节点。用一个数组来统计每个点的入度,vector保存出度的节点,然后就可以开始判断。在判断的时候,将与这个点去掉,就是指这个点连接的所有点的入度都减了1。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,m,u,v;
int in[],inx[];
vector<int>out[];
int main(){
cin>>n>>m;
memset(in,,sizeof(in));
for(int i=;i<=m;i++){
cin>>u>>v;
out[u].push_back(v);//保存出去的节点
in[v]++; //计算入度
}
int k;
cin>>k;
int a[];
int num=;
for(int i=;i<k;i++){
int f=;
memcpy(inx, in, sizeof(in));//将in拷贝给inx
for(int j=;j<=n;j++){
cin>>u;
if(inx[u]!=||f==){
f=;
continue;
}
for(int p=;p<out[u].size();p++){//对受影响的节点的入度--
inx[out[u].at(p)]--;
}
}
if(!f){
a[++num]=i;
}
}
for(int i=;i<=num;i++){
cout<<a[i];
if(i!=num) cout<<" ";
}
return ;
}

PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)的更多相关文章

  1. PAT甲级——1146 Topological Order (25分)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  2. PAT 甲级 1146 Topological Order

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...

  3. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  4. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

  5. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  6. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  7. PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

    1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ord ...

  8. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  9. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

随机推荐

  1. vue PC端,用到的知识

    1.vue中通过路由跳转的三种方式      https://blog.csdn.net/qq_40072782/article/details/82533477 2.数组解构,对象解构:https: ...

  2. 使用笔记:TF辅助工具--tensorflow slim(TF-Slim)

    如果抛开Keras,TensorLayer,tfLearn,tensroflow 能否写出简介的代码? 可以!slim这个模块是在16年新推出的,其主要目的是来做所谓的“代码瘦身” 一.简介 slim ...

  3. GBDT 算法

    GBDT (Gradient Boosting Decision Tree) 梯度提升迭代决策树.GBDT 也是 Boosting 算法的一种,但是和 AdaBoost 算法不同(AdaBoost 算 ...

  4. PHP流程控制之goto语法

    自 PHP 5.3.0 起,还可以使用 goto 来跳出循环. 在本章开始的章节,我们讲解到一个故事,王同学每周往返,但有一个特例:直线电机滑台 项目失败后或者集团临时除知除外,他就可以不再这么每周往 ...

  5. 洛谷 P1886 滑动窗口 题解

    每日一题 day26 打卡 Analysis 单调队列模板 对于每一个区间,有以下操作: 1.维护队首(就是如果你已经是当前的m个之前那你就可以被删了,head++) 2.在队尾插入(每插入一个就要从 ...

  6. input重复上传图片失效的问题

    使用input的file来上传图片 如果是重复选择相同的图片就会失效 解决的办法就是当每次上传完图片后让input的值为空就可以解决了 附上代码 只需在末尾添加这句就行$(this).val(&quo ...

  7. LIO -SCSI target

    2010年底,LIO 项目获选成为新的内核态的 SCSI target,取代原有的用户态的 STGT 项目.当时有两个主要的竞争项目(LIO和SCST),都在努力将代码并入主线内核.本文将比较着两个项 ...

  8. mysql 运算操作符

    Name Description AND, && Logical AND = Assign a value (as part of a SET statement, or as par ...

  9. P1903 [国家集训队]数颜色 (带修改莫队)

    题目描述 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔. 2 ...

  10. 1、vueJs基础知识01

    vue是框架,vue.js是vue框架的核心js库 库:是一个封装好的特定的方法的集合,提供给开发者使用,库没有控制权,控制权在使用者手中.代表:jQuery.underscore.util 框架:框 ...