https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760

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 <bits/stdc++.h>
using namespace std; int N, M, K;
int topo[1010][1010];
int num[1010];
map<int, int> mp; int main() {
scanf("%d%d", &N, &M);
memset(topo, 0, sizeof(topo));
while(M --) {
int a, b;
scanf("%d%d", &a, &b);
topo[a][b] = 1;
} scanf("%d", &K);
vector<int> ans;
for(int k = 0; k < K; k ++) {
bool flag = true;
for(int i = 0; i < N; i ++)
scanf("%d", &num[i]); for(int i = N - 1; i >= 1; i --) {
for(int j = i - 1; j >= 0; j --) {
if(topo[num[i]][num[j]]) {
flag = false;
break;
}
}
}
if(!flag) ans.push_back(k);
} for(int i = 0; i < ans.size(); i ++)
printf("%d%s", ans[i], i != ans.size() - 1 ? " " : "\n");
return 0;
}

  建立有向图 输入的每一组数据从后向前暴力如果走得通的话就是 false

FHFHFH

PAT 甲级 1146 Topological Order的更多相关文章

  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. PAT 1146 Topological Order[难]

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

  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 1146 Topological Order

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

  6. 1146. Topological Order (25)

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

  7. 1146 Topological Order

    题意:判断序列是否为拓扑序列. 思路:理解什么是拓扑排序就好了,简单题.需要注意的地方就是,因为这里要判断多个,每次判断都会改变入度indegree[],因此记得要把indegree[]留个备份.ps ...

  8. PAT甲级目录

    树(23) 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree 判断BST,BST的性质 ...

  9. PAT A1146 Topological Order (25 分)——拓扑排序,入度

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

随机推荐

  1. 20155211 课下测试ch12补做

    20155211 课下测试ch12补做 有关线程图,下面说法正确的是() A.图的原点表示没有任何线程完成一条指令的初始状态 B.向右向上是合法的转换 C.向左向下是合法的转换 D.对角线是合法的转换 ...

  2. 《记忆匣子》——网络编程jsp网页制作复习习笔记1

    内容都在图里 哈哈哈 <%@ page language="java" contentType="text/html; charset=utf-8" pa ...

  3. python基础学习1-第一个网络爬虫程序

    #!/usr/bin/env python # -*- coding:utf-8 -*- 煎蛋网抓妹子图 import urllib.request import os import random d ...

  4. python爬虫之scrapy框架介绍

    一.什么是Scrapy? Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等) ...

  5. 2018.4.23 linux系统

    linux: 1.代表linux的内核 2.代表linux的操作系统:linux的内核和工具软件.应用软件..办公工具.开发工具. 它的特点: 1.它是开源软件,时当今最成功的开源软件之一.所以很多的 ...

  6. 我用Python远程探查室友的网页浏览记录,他不愧是成年人!

    过程: 利用Python制作远程查看别人电脑的操作记录,与其它教程类似,都是通过邮件返回. 利用程序得到目标电脑浏览器当中的访问记录,生产一个文本并发送到你自己的邮箱,当然这个整个过程除了你把pyth ...

  7. SQL Operations Studio的安装和使用

    之前管理和访问SQL SERVER使用的自然是SSMS,功能确实很强大的一个数据库图形化管理软件,但是SSMS有个问题就是体积超级大,启动速度也就比较慢.今天我正好要学习一些T-SQL的内容,在微软的 ...

  8. 1042 Shuffling Machine

    一.题目描述 Shuffling is a procedure used to randomize a deck of playing cards. Because standard shufflin ...

  9. 配置tensorflow环境(anaconda+jupyter notebook)

    很早之前,tensorflow环境之前我也曾装过,但是用的不是很舒服,很多问题都不明所以然.今天想要系统地学习一下tensorflow,于是又重新搭建了一遍,这次还是踩了不少坑.特此写下此文,供有兴趣 ...

  10. linux 安装配置zookeeper脚本

    #!/bin/bash # automatic install zookeeper echo "========= Start to install zookeeper ========== ...