【PAT甲级】1146 Topological Order

简介: 【PAT甲级】1146 Topological Order

1146 Topological Order

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
6
5 2 3 6 4 1
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:

0 4 5


题意

给定一个有向图,并给定多个序列,需要我们判断这些序列是否是拓扑序列,将不是拓扑序列的询问编号输出,询问编号从 0 开始。


拿题目样例来看,序列 [5 2 1 6 3 2] 之所以不是拓扑序列,是因为其中 2->1 方向反了。


图中只有 1->2 的边但没有 2->1 的边,不能出现逆向边,也就是每条边的前面那个点在序列中的位置必须在后面那个点的位置之前。


思路

我们可以通过记录给定序列每个元素对应的下标,然后去判断每条边的两个端点的下标大小,如果是拓扑序列,则每条边 a->b 中 a 在序列中的下标必须比 b 在序列中的下标小,即 a 在序列中的位置必须在 b 之前,否则就不是一个拓扑序列。


假设还是样例中的那张图,现在给定一个序列 [5 2 3 6 4 1] ,可以找到一条边 1->2 ,发现 1 在序列中的第 6 位,而 2 却在第 2 位,即 1 出现在了 2 的后面,所以该序列不是拓扑序列。


另外需要注意的是,输出的询问编号是从 0 开始,并且输出的末尾不能出现空格。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 10010;
struct Edge {
    int a, b;
}e[M];
int p[N];
int main()
{
    int n, m;
    cin >> n >> m;
    //输入每条有向边
    for (int i = 0; i < m; i++)    cin >> e[i].a >> e[i].b;
    //开始查询
    bool is_first = true; //输出行末不能有空格
    int k;
    cin >> k;
    for (int i = 0; i < k; i++)
    {
        //输入每个序列
        for (int j = 1; j <= n; j++)
        {
            int x;
            cin >> x;
            p[x] = j; //记录每个点所在序列下标
        }
        //遍历每条边,判断是否都满足拓扑序列规则
        bool success = true;
        for (int i = 0; i < m; i++)
            if (p[e[i].a] > p[e[i].b])
            {
                success = false;
                break;
            }
        //输出不是拓扑序列的询问编号
        if (!success)
        {
            if (is_first)    is_first = false;
            else    cout << " ";
            cout << i;
        }
    }
    cout << endl;
    return 0;
}


目录
相关文章
|
4天前
|
人工智能 自然语言处理 Shell
🦞 如何在 Moltbot 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
🦞 如何在 Moltbot 配置阿里云百炼 API
|
8天前
|
人工智能 API 开发者
Claude Code 国内保姆级使用指南:实测 GLM-4.7 与 Claude Opus 4.5 全方案解
Claude Code是Anthropic推出的编程AI代理工具。2026年国内开发者可通过配置`ANTHROPIC_BASE_URL`实现本地化接入:①极速平替——用Qwen Code v0.5.0或GLM-4.7,毫秒响应,适合日常编码;②满血原版——经灵芽API中转调用Claude Opus 4.5,胜任复杂架构与深度推理。
|
2天前
|
人工智能 JavaScript 应用服务中间件
零门槛部署本地AI助手:Windows系统Moltbot(Clawdbot)保姆级教程
Moltbot(原Clawdbot)是一款功能全面的智能体AI助手,不仅能通过聊天互动响应需求,还具备“动手”和“跑腿”能力——“手”可读写本地文件、执行代码、操控命令行,“脚”能联网搜索、访问网页并分析内容,“大脑”则可接入Qwen、OpenAI等云端API,或利用本地GPU运行模型。本教程专为Windows系统用户打造,从环境搭建到问题排查,详细拆解全流程,即使无技术基础也能顺利部署本地AI助理。
4256 5
|
2天前
|
人工智能 JavaScript API
零门槛部署本地 AI 助手:Clawdbot/Meltbot 部署深度保姆级教程
Clawdbot(Moltbot)是一款智能体AI助手,具备“手”(读写文件、执行代码)、“脚”(联网搜索、分析网页)和“脑”(接入Qwen/OpenAI等API或本地GPU模型)。本指南详解Windows下从Node.js环境搭建、一键安装到Token配置的全流程,助你快速部署本地AI助理。(239字)
2648 15
|
3天前
|
机器人 API 数据安全/隐私保护
只需3步,无影云电脑一键部署Moltbot(Clawdbot)
本指南详解Moltbot(Clawdbot)部署全流程:一、购买无影云电脑Moltbot专属套餐(含2000核时);二、下载客户端并配置百炼API Key、钉钉APP KEY及QQ通道;三、验证钉钉/群聊交互。支持多端,7×24运行可关闭休眠。
3098 4
|
3天前
|
人工智能 安全 Shell
在 Moltbot (Clawdbot) 里配置调用阿里云百炼 API 完整教程
Moltbot(原Clawdbot)是一款开源AI个人助手,支持通过自然语言控制设备、处理自动化任务,兼容Qwen、Claude、GPT等主流大语言模型。若需在Moltbot中调用阿里云百炼提供的模型能力(如通义千问3系列),需完成API配置、环境变量设置、配置文件编辑等步骤。本文将严格遵循原教程逻辑,用通俗易懂的语言拆解完整流程,涵盖前置条件、安装部署、API获取、配置验证等核心环节,确保不改变原意且无营销表述。
1775 3
|
12天前
|
JSON API 数据格式
OpenCode入门使用教程
本教程介绍如何通过安装OpenCode并配置Canopy Wave API来使用开源模型。首先全局安装OpenCode,然后设置API密钥并创建配置文件,最后在控制台中连接模型并开始交互。
5165 8
|
3天前
|
存储 安全 数据库
使用 Docker 部署 Clawdbot(官方推荐方式)
Clawdbot 是一款开源、本地运行的个人AI助手,支持 WhatsApp、Telegram、Slack 等十余种通信渠道,兼容 macOS/iOS/Android,可渲染实时 Canvas 界面。本文提供基于 Docker Compose 的生产级部署指南,涵盖安全配置、持久化、备份、监控等关键运维实践(官方无预构建镜像,需源码本地构建)。
2136 6
|
3天前
|
人工智能 应用服务中间件 API
刚刚,阿里云上线Clawdbot全套云服务!
阿里云上线Moltbot(原Clawdbot)全套云服务,支持轻量服务器/无影云电脑一键部署,可调用百炼平台百余款千问模型,打通iMessage与钉钉消息通道,打造开箱即用的AI智能体助手。
2320 18
刚刚,阿里云上线Clawdbot全套云服务!
|
3天前
|
人工智能 安全 应用服务中间件
首个 Clawdbot 全流程部署方案!真“AI 个人助理”来了!
GitHub爆火AI Agent Moltbot(原Clawdbot)上线即获7.6万+ Star!它能理解自然语言、调用工具、自动执行任务。阿里云轻量应用服务器推出“开箱即用”部署方案:预装环境、直连百炼大模型、支持钉钉等消息通道,5分钟一键启用,稳定、安全、低成本。
首个 Clawdbot 全流程部署方案!真“AI 个人助理”来了!

热门文章

最新文章