当前位置:网站首页>1146 Topological Order (25 分)
1146 Topological Order (25 分)
2022-06-29 09:23:00 【陌陌623】
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
给一个图,然后一些序列,判断序列是不是整个图的拓扑排序
判断方法:
- 在输入的时候记录每个节点入读
- 在判断序列时,依次将某一个点x出去的点y的入度减一,说明是x点在y点的前面,如果到达了y点,y的入度仍然不是0,说明本应该在y点前面的点,现在肯定是在y’点的后面。
#include <iostream>
#include <vector>
using namespace std;
const int n = 1e4 + 10;
int N, M, x, y, f;
vector<int> v[n];
int in[n];
int main()
{
cin >> N >> M;
while (M--)
{
cin >> x >> y;
v[x].push_back(y);
// 记录入度
in[y]++;
}
cin >> M;
for (int i = 0; i < M; i++)
{
// 每次in都会改变
vector<int> tin(in, in + N + 1);
int judge = 0;
for (int j = 0; j < N; j++)
{
cin >> x;
if (tin[x])
judge = 1;
for (auto &e : v[x])
tin[e]--;
}
if (judge == 0)
continue;
if (f++ != 0)
cout << " ";
cout << i;
}
return 0;
}
边栏推荐
- Codeforces Round #659 (Div. 2)
- LiferayPortal JSONWS反序列化漏洞(CVE-2020-7961)分析
- 详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
- 2020-09-18 referer认证 url转义
- If I were in Beijing, where would it be better to open an account? In addition, is it safe to open an account online now?
- Leetcode MySQL database topic 177
- Alibaba cloud firewall configuration, multiple settings (iptables and firewall)
- Leetcode MySQL database topic 181
- 聊聊你理解的线程与并发
- 2019.10.20训练总结
猜你喜欢

自定义控件之下载控件1(DownloadView1)

图片验证码控件

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

完美二叉树、完全二叉树、完满二叉树

Alternative implementation of Scrollview pull-down header amplification

Alibaba cloud firewall configuration, multiple settings (iptables and firewall)

Listview of the basic component of the shutter

弧形 View 和弧形 ViewPager

float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”

520 钻石争霸赛 2021
随机推荐
51nod1277 字符串中的最大值【KMP】
Leetcode MySQL database topic 178
Introduction to intranet penetration tool FRP
动态规划总结
弧形 View 和弧形 ViewPager
Memory layout of JVM objects
Flutter 基础组件之 Text
1099 Build A Binary Search Tree (30 分)
Pipeline details of IPC (interprocess communication)
2020-09-21 referer字符串切分 boost gateway代码组织层次
ImageView picture fill problem
leetcode MYSQL数据库题目181
setInterval、setTimeout和requestAnimationFrame
JVM之 MinorGC、 MajorGC、 FullGC、
2019.10.27训练总结
1098 Insertion or Heap Sort (25 分)
XML布局中Button总是在最上层显示
C语言实现一种创建易管理易维护线程的方法
PHP内存马技术研究与查杀方法总结
2020-9-14 广告系统入门