PAT_A1146#Topological Order
Source:
Description:
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
Keys:
Attention:
- 性质:若图顶点按照拓扑排序重新编号,则存储矩阵为上三角阵;
- 由性质可以推断出,若存储图的矩阵为三角阵,则存在拓扑排序,反之不一定成立;
- 拓扑排序算法本身比较简单,了解概念之后自然就可以写出了~
Code:
/*
Data: 2019-05-19 20:41:28
Problem: PAT_A1146#Topological Order
AC: 22:04 题目大意:
判别拓扑排序
输入:
第一行给出顶点数N<=1e3,和边数M<=1e4
接下来M行,给出顶点及其有向边(顶点从1~N)
接下来给出查询次数K
接下来K行给出顶点序列
输出:
按序号给出不是拓扑排序的序列(0~K-1)
*/ #include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int M=1e3+;
vector<int> grap[M],id(M),ans; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,m,u,v,s[M];
scanf("%d%d", &n,&m);
fill(id.begin(),id.end(),);
for(int i=; i<m; i++)
{
scanf("%d%d",&v,&u);
grap[v].push_back(u);
id[u]++;
}
scanf("%d", &m);
for(int i=; i<m; i++)
{
vector<int> d = id;
for(int j=; j<n; j++)
scanf("%d", &s[j]);
for(int j=; j<n; j++)
{
v=s[j];
if(d[v]==)
for(int k=; k<grap[v].size(); k++)
d[grap[v][k]]--;
else
{
ans.push_back(i);
break;
}
}
}
for(int i=; i<ans.size(); i++)
printf("%d%c", ans[i],i==ans.size()-?'\n':' '); return ;
}
PAT_A1146#Topological Order的更多相关文章
- A1146. Topological Order
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 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 ...
- PAT 甲级 1146 Topological Order
https://pintia.cn/problem-sets/994805342720868352/problems/994805343043829760 This is a problem give ...
- PAT 1146 Topological Order[难]
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...
- [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 1146 Topological Order
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- 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 Is Topological Order
Write a program to test if a give sequence Seq is a topological order of a given graph Graph. Format ...
随机推荐
- Spring MVC-控制器(Controller)-属性方法名称解析器(Properties Method Name Resolver )示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_propertiesmethodnameresolver.htm 说明:示例基于S ...
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined
Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined stac ...
- cocos2d-html5开发之本地数据存储
做游戏时常常须要的一个功能呢就是数据的保存了,比方游戏最高分.得到的金币数.物品的数量等等.cocos2d-html5使用了html5.所以html5的数据保存方法是对引擎可用的: html5本地数据 ...
- Instagram的Material Design概念设计文章分享
近期開始研究最新的Android 5 Material Design,一加氢OS公布后,非常快就有一大批支持Android5原生风格的手机出来了,你的App还是UI帮设计的吗?该考虑升级到 Mater ...
- Codeforces Round #281 (Div. 2) D. Vasya and Chess 博弈
D. Vasya and Chess Vasya decided to learn to play chess. Classic chess doesn't seem interesting to ...
- linux中两个缓冲区
不同于Windows,Linux系统里存在两个剪切板:一个叫做选择缓冲区(X11 selection buffer),另一个才是剪切板(clipboard). 01)选择缓冲区(缓冲内容在其他位置可用 ...
- P1155 双栈排序(二分图染色)
P1155 双栈排序(二分图染色) 题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一 ...
- PCB 周期日历
在PCB行业一直以来没找到适合我们这行业的日历,主要存在2个差异导致. 1.周期差异:由于PCB 周期计算的复杂性,市面上无法找到符合PCB行业计算周期方式 (另一遍博文中有写周期计算逻辑) http ...
- [Apple开发者帐户帮助]三、创建证书(4)创建Safari签名证书
您的Safari扩展程序必须由Apple颁发的证书签名,您可以在开发者帐户中创建和下载该证书. 在“ 证书”,“标识符和配置文件”中,从左侧的弹出菜单中选择“Safari扩展”. 在“证书”下,选择“ ...
- ASP.NET Core 多环境
ASP.NET Core 支持在多个环境中管理应用程序,如开发(Development),预演(Staging)和生产(Production).环境变量用来指示应用程序正在运行的环境,允许应用程序适当 ...