[PAT] 1146 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
题意:
给出一个有向图,并给出k个查询,输出所有不是拓扑排序的查查询。
思路:
1.记录每个点的next,入度。
2.vector拷贝的相关操作:C++ vector拷贝使用总结
题解:
#include<cstdlib>
#include<cstdio>
#include<vector>
using namespace std;
//定义每个节点的入度和对应出去的节点
struct node
{
;
vector<int> out;
};
int main() {
int n, m;
scanf("%d %d", &n, &m);
vector<node> nodes(n + );
int a, b;
; i < m; i++) {
scanf("%d %d", &a, &b);
nodes[a].out.push_back(b);
nodes[b].in++;
}
int query;
scanf("%d", &query);
;
; i < query; i++) {
bool flag = true;
//每次查询对nodes的副本进行修改。
vector<node> tNodes(nodes);
; j < n; j++) {
int t;
scanf("%d", &t);
//即使已经判断出来不是拓扑排序了,仍然需要接受剩下的输入。
if (!flag) continue;
) {
; k < tNodes[t].out.size(); k++) {
tNodes[tNodes[t].out[k]].in--;
}
}
else {
) printf(" ");
printf("%d", i);
cnt++;
flag = false;
}
}
}
;
}
[PAT] 1146 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 ...
- 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
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- 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 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 1051 Pop Sequence (25 分)
返回 1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ...
随机推荐
- BZOJ2668 [cqoi2012]交换棋子 【费用流】
题目链接 BZOJ2668 题解 容易想到由\(S\)向初始的黑点连边,由终态的黑点向\(T\)连边,然后相邻的点间连边 但是这样满足不了交换次数的限制,也无法计算答案 考虑如何满足一个点的交换次数限 ...
- JavaScript非阻塞加载脚本
As more and more sites evolve into “Web 2.0″ apps, the amount of JavaScript increases. This is a per ...
- linux 常见服务端口
Linux服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程(daemons) 来执行的.守护进 ...
- 软银开放Pepper开发,给机器人写安卓App是怎样一种体验?
日本软银推出的Pepper智能机器人 新浪科技讯 北京时间5月19日下午消息,谷歌Android移动操作系统的触角正在不断扩大,从今天开始,开发者可以为日本电信公司软银的Pepper人形机器人设计An ...
- pushViewController:animated:的问题
1.在AppDelegate.m中: 2.在SecondViewController.h中: 3.在FirstViewController.m中: 4.在SecondViewController.m中 ...
- 搭建JavaWeb应用开发环境
下载和安装Tomcat服务器 下载Tomcat安装程序包:http://tomcat.apache.org/,下载一个zip版本,解压到本地即完成了Tomcat的安装. 测试是否安装成功:进入Tomc ...
- ioctrl 获取本机IP及MAC地址
通过使用ioctl可以获得本机的一些信息,这里记录获得interface IP及MAC的过程. 1:ioctl 函数的作用是什么 man ioctl: DESCRIPTION The ioctl() ...
- ExtJS下拉列表使用方法(异步传输数据)
最近使用ExtJS下拉列表框(ComboBox)希望完成一个动态下拉列表的功能,即列表中的数据都通过异步方式查询数据库而来,同时在用户选择了列表中的某个值后,可以从后台正确的获取用户选择项所对应的值. ...
- Lodash js数据操作库
https://lodash.com/docs/4.17.4
- MyBatis框架的使用及源码分析(三) 配置篇 Configuration
从上文<MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder> 我们知道XMLConf ...