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 <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=;
int a[maxn] ;
int n,m,k;
vector<int> v;
vector<int> adj[maxn];
int degree[maxn]={};
int store[maxn]={};
int main(){
scanf("%d %d",&n,&m);
for(int i=;i<=m;i++){
int c1,c2;
scanf("%d %d",&c1,&c2);
degree[c2]++;
adj[c1].push_back(c2);
}
scanf("%d",&k);
for(int i=;i<k;i++){
for(int j=;j<=n;j++){
store[j]=degree[j];
}
int flag=;
for(int j=;j<n;j++){
int tmp;
scanf("%d",&tmp);
if(flag==){
continue;
}
else{
if(store[tmp]==){
for(int q=;q<adj[tmp].size();q++){
store[adj[tmp][q]]--;
}
}
else{
flag=;
}
}
}
if(flag==)v.push_back(i);
}
for(int i=;i<v.size();i++){
printf("%d%s",v[i],i==v.size()-?"\n":" ");
}
}

注意点:一开始没头绪要怎么做,翻了翻算法笔记,看了大佬的思路,发现原来这么方便,只要看进来的这个数入度是否为0。有一个坑就是不要一发现一个数不满足条件了就break,这样后面的输入就读不到了,要记录状态continue

PAT A1146 Topological Order (25 分)——拓扑排序,入度的更多相关文章

  1. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  2. 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 ...

  3. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  4. [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 ...

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

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

  6. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  7. PAT 1146 Topological Order[难]

    1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...

  8. A1146. Topological Order

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

  9. 6-06. 理性任务调度(25)(拓扑排序啊 ZJU_PAT)

    主题链接:http://pat.zju.edu.cn/contests/ds/6-06 假定一个project项目由一组子任务构成,子任务之间有的能够并行运行.有的必须在完毕了其他一些子任务后才干运行 ...

随机推荐

  1. 使用Swagger2时遇到的问题

    Swagger2使用起来很简单,加一个@EnableSwagger2注解,并引入如下依赖就ok了 <dependency> <groupId>io.springfox</ ...

  2. Hash table lengths and prime numbers

    Website:http://srinvis.blogspot.ca/2006/07/hash-table-lengths-and-prime-numbers.html This has been b ...

  3. netty 基础

    Netty是一个高性能.异步事件驱动的NIO框架,提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户 ...

  4. Python全栈学习_day004作业

    ,写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian", "barry&qu ...

  5. Mysql数据库的(行记录)详细操作

    在Mysql管理软件中, 可以通过sql语句中的dml语言来实现数据的操作, 包括 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 使用SELECT查询数据 ...

  6. The Minimum Cycle Mean in a Digraph 《有向图中的最小平均权值回路》 Karp

    文件链接 Karp在1977年的论文,讲述了一种\(O(nm)\)的算法,用来求有向强连通图中最小平均权值回路(具体问题请参照这里) 本人翻译(有删改): 首先任取一个节点 \(s\) ,定义 \(F ...

  7. MVC与单元测试实践之健身网站(七)-添加计划

    ​计划的制定涉及到周期-动作包-动作的关联操作,在上一篇<计划的添加与重置>完成了周期的设置.动作包的添加,现在要完成的是动作的添加操作. 一 具体功能 a) 在选定了一个大周期具有的天数 ...

  8. Django 信息

    Request information USER admin GET No GET data POST No POST data FILES No FILES data COOKIES Variabl ...

  9. 13.2、进程的通信:Queue、Pipe、

    内容相关: 概念:进程的通信 Queue:创建与使用 Pipe:创建与使用 进程通信的概念 进程的资源空间是相互独立的,一般而言是不能相互访问的.但很多情况下进程间需要互相通信,来完成系统的某项功能. ...

  10. 关于《SQLSERVER走起》微信账号自动回复功能的升级

    关于SQLSERVER走起微信账号自动回复功能的升级 由于腾讯对微信公众账号的升级,本公众账号也增加了关键词自动回复功能, 只需要输入某些特定关键词,本公众账号就会进行自动回复. 例如: 输入 sql ...