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. eclipse中安装windowbuilder插件、应用及卸载

    一.安装windowbuilder插件 1.找到对应版本的windowbuilder 连接地址:http://www.eclipse.org/windowbuilder/download.php 如图 ...

  2. Java-关于类

    java-关于类-成员初始化问题 成员初始化出现错误“    - Syntax error on token ";", , expected” java类不允许对成员进行操作,但可 ...

  3. PHP7.27: connect mysql 5.7 using new mysqli

    <!doctype html> <html> <head> <meta name="viewport" content="wid ...

  4. 洛谷P4007 小 Y 和恐怖的奴隶主(期望dp 矩阵乘法)

    题意 题目链接 Sol 首先不难想到一种暴力dp,设\(f[i][a][b][c]\)表示还有\(i\)轮没打,场上有\(a\)个1血,\(b\)个2血,\(c\)个三血 发现状态数只有\(s = 1 ...

  5. easyUI combobox combotree 模糊查询,带上下键选择功能,待完善。。。。

    /2017年4月9日 11:52:36 /** * combobox和combotree模糊查询 * combotree 结果带两级父节点(手动设置数量) * 键盘上下键选择叶子节点 * 键盘回车键设 ...

  6. Html5画钟表盘/指针实时跳动

    1.最终效果 时钟.分钟.秒指针连续移动2.完整代码 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml&q ...

  7. Ubuntu 18 + Redis安装

    Ubuntu 18 + Redis安装 1.安装命令: opengis@gisserver20:~$ sudo apt-get install redis-server 2.查看tcp 连接 open ...

  8. python练习(-)

    简单的爬虫示例: import urllib.request #python2.x版本为urllib2url = 'http://www.douban.com/'webPage=urllib.requ ...

  9. Linux 学习笔记之超详细基础linux命令 Part 11

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 10---------------- ...

  10. Android RecycleView 自定义Item的使用

    自定义布局的RecycleView需要自己实现Adapter,ViewHolder和布局: 自定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBin ...