PAT A1146 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 <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 分)——拓扑排序,入度的更多相关文章
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- 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 ...
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- [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 ...
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 1146 Topological Order[难]
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...
- A1146. Topological Order
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 6-06. 理性任务调度(25)(拓扑排序啊 ZJU_PAT)
主题链接:http://pat.zju.edu.cn/contests/ds/6-06 假定一个project项目由一组子任务构成,子任务之间有的能够并行运行.有的必须在完毕了其他一些子任务后才干运行 ...
随机推荐
- 阿里CentOS 7 卸载mysql5.6
查看当前安装mysql情况 rpm -qa|grep -i mysql 执行 yum remove mysql rpm -e mysql-community-release-el7-5.noarch ...
- 设计模式之命令模式(Command )
命令模式是我们能够实现发送者和接收者之间的完全解耦,发送者是调用操作的对象,而接收者是接收请求并执行特定操作的对象.通过解耦,发送者无需了解接收者的接口.在这里,请求的含义是需要被执行的命令. 作用 ...
- ionic3打包不能prod的问题
在最近的项目中,我ionic3采用了懒加载,来提高性能.但是当我普通打包的时候,正常成功了,但是加上--prod的时候,就报错了. 报错如下: 大概意思就是page是声明的一部分,然后请在更高级声明之 ...
- Jmeter在非GUI(命令行)模式下生成测试报告
根据各大招聘网站上的需求来看,熟悉Jmeter做性能测试已经几乎成为必要条件了. 那么今天在这个给大家安利一波,怎么使用Jmeter在非GUI(命令行)模式下生成测试报告呢?? 条件准备: 1.Jme ...
- Android Studio 通过一个登录功能介绍SQLite数据库的使用
前言: SQLite简介:是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在 ...
- Loading加载页面
一般页面有四种情况 加载中 :就是滚动页面,后台获取加载的数据,每个页面的数据不同所以就让子类来实现,直接抽象abstract了. 加载失败 :一般都需要点击后重新加载 空页面 :也需要点击后重新加载 ...
- bash shell下最方便的字符串大小写转换方法
用tr需要新增变量,用declare或typeset需要在变量赋值前或者赋值后单独声明,都有些麻烦 此方法为bash 4.0以后新增,bash 4.0 2009年发布 $ test="abc ...
- 利用python中的gensim模块训练和测试word2vec
word2vec的基础知识介绍参考上一篇博客和列举的参考资料. 首先利用安装gensim模块,相关依赖如下,注意版本要一致: Python >= 2.7 (tested with version ...
- 第四周读书笔记——读《我是一只IT小小鸟》有感
读<我是一只IT小小鸟>有感 这是邓老师倾力推荐的一本书.这本书的标题化用了我们耳熟能详的歌词,算是较有新意吧.更重点在于,这本书的作者不是哪一位大牛,而是一群刚刚走出校 ...
- Python之groupby
# -*- coding: utf-8 -*-"""Created on Sat Jun 30 10:09:47 2018测试分组groupby@author: zhen ...